junit学习(四)——Junit的运行过程
直接用代码来说明吧。
1、先在test资源包里新建一个Junit Test Case,与前面建的Test Case不同的是,得勾选如图的几个方法:
有什么用???后面就明白啦。
我这里的Test Case 叫JunitFollowTest,Finish之后得到的代码如下:
package com.wjl.junit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
public class JunitFollowTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
}
2、在各个方法里边System.out.println()点什么,然后添加几个自定义的test方法,在test方法里边也随便输出点什么,最后的代码如下:
package com.wjl.junit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Junit_demo_4
* 实验 JUnit的运行过程
* **/
public class JunitFollowTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("BeforeClass......");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("AfterClass......");
}
@Before
public void setUp() throws Exception {
System.out.println("Before.......");
}
@After
public void tearDown() throws Exception {
System.out.println("After......");
}
//写个测试方法
@Test
public void testJunitFollow(){
System.out.println("testJunitFollow().......");
}
@Test
public void testJunitFollow2(){
System.out.println("testJunitFollow2().......");
}
}
运行结果如下:
看到这个结果,应该知道那四个方法干什么用的了吧,也明白Junit的运行过程了吧。
总结:
1、@BeforeClass修饰的方法会在所有方法被调用之前被执行,而且该方法是静态的,所以当测试类被加载后接着就会运行它,而且在内存中它只存在一份实例,它比较适合加载配置文件
2、@AfterClass修改的方法会在所有方法被执行完成之后被执行,通常用来对资源的清理,如关闭数据库的连接
3、@Before和@After会在每个测试方法的前后各执行一次。
最后,感谢老师的分享!