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会在每个测试方法的前后各执行一次。

 

最后,感谢老师的分享!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值