Junit4学习(三)Junit运行流程

一,验证Junit测试方法的流程数据库

1,在test/com.duo.util右键,新建测试类测试

2,生成后的代码:spa

 1 package com.duo.util;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.After;
 6 import org.junit.AfterClass;
 7 import org.junit.Before;
 8 import org.junit.BeforeClass;
 9 import org.junit.Test;
10 
11 public class JunitFlowTest {
12 
13     @BeforeClass
14     public static void setUpBeforeClass() throws Exception {
15         System.out.println("This is @BeforeClass...");
16     }
17 
18     @AfterClass
19     public static void tearDownAfterClass() throws Exception {
20         System.out.println("This is AfterClass...");
21     }
22 
23     @Before
24     public void setUp() throws Exception {
25         System.out.println("This is Before...");
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30         System.out.println("This is After...");
31     }
32     
33     @Test
34     public void test1(){
35         System.out.println("This is test1...");
36     }
37     
38     @Test
39     public void test2(){
40         System.out.println("This is test2...");
41     }
42 
43 }

运行结果:code

This is @BeforeClass...
This is Before...
This is test1...
This is After...
This is Before...
This is test2...
This is After...
This is AfterClass...

二,总结:blog

1,@BeforeClass修饰的方法会在全部方法被调用前被执行;并且该方法是静态的,因此当测试类被加载后接着就会运行它,并且在内存中它只会存在一份实例,它比较适合加载配置文件;好比数据的链接文件等;内存

2,@AfterClass所修饰的方法一般用来对资源的清理,如数据库的关闭;资源

3,@Before和@After会在每一个测试方法先后执行;一般被称为固定代码(fixure),就是必定会执行的代码.it

相关文章
相关标签/搜索