Junit的简单实用(环境Eclipse)

当想要对方法进行单独的测试的时候不妨试试使用Junit

使用方法

/**
 *
 * @author zero
 * 1. ProjectName-->build path-->configure build path-->add library-->JUnit-->choose version

在项目名称上右键按照上述的目录结构添加JUnit工具包
 * 2. add annotation @Test on your method that needs to be test Ctrl+Shift+O import relevant class

在需要单元测试的方法上添加@Test注解 并使用快捷键Ctrl+Shift+O引入相关的包文件
 * 3. click the right key of your mouse and choose run as Junit test
 *保存后  单击鼠标右键选择用JUnit运行程序,这样一来,被Test注解的方法就会被执行

 * besides of that, there are two annotations can be used to test @Before and @After

另外,在Junit版本4中还有@Before 和 @After 两种新的注解,分别在@Test注解的方法前后执行

 * executing steps : @Before  @Test @After
 * tips: @Before and @After only can be used in JUnit version 4
 */


import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 
 * @author zero
 * 1. ProjectName-->build path-->configure build path-->add library-->JUnit-->choose version
 * 2. add annotation @Test on your method that needs to be test Ctrl+Shift+O import relevant class
 * 3. click the right key of your mouse and choose run as Junit test 
 * 
 * besides of that, there are two annotations can be used to test @Before and @After
 * executing steps : @Before  @Test @After
 * tips: @Before and @After only can be used in JUnit version 4
 */
public class Junit {
	
	@Before
	public void beforeTest() {
		System.out.println("This method is called before Test method");
	}
	
	@After
	public void afterTest() {
		System.out.println("This method is called after Test method");
	}
	
	
	@Test
	public void unitTest() {
		System.out.println("run the method lonely");
	}
	
	public static void main(String[] args) {
	
	}
}

执行结果为:

This method is called before Test method
run the method lonely
This method is called after Test method

以上就是单元测试的基本使用方法(这是在Eclipse环境的测试)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值