Junit学习笔记(四)

Junit中annotation的简单使用用例


当需要同时运行多个Junit的测试类时,可以选中一个测试类(一个包,或者一个工程都可以),点击右键 --> Run as --> Run Configurations

选中<Run all tests in the selected projcet, package or source folder> ,点击 OK 即可

 
 
/**
 * 
 * @author KerYang
 * 
 */

package com.keryang.junit.instance.junittest;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.keryang.junit.instance.AnnotationInt;

public class AnnotationIntTest {

	// 抛出异常java.lang.ArithmeticException,被测试方法(devide)的执行时间不能超过100毫秒,否则测试不通过
	@Test(expected=java.lang.ArithmeticException.class, timeout=100)
	public void testDevide() {
		
		AnnotationInt ai = new AnnotationInt();
		assertThat(4, is(ai.devide(8, 0)));
		
	}
	
	// 执行该测试类时,暂不执行该测试方法testPlus
	@Ignore
	@Test
	public void testIngore () {
		
		System.out.println("ingore");
		
	}
	
	
	@Test
	public void testPlus () {
		
		AnnotationInt ai = new AnnotationInt();
		assertThat(4, is(ai.plus(8, -4)));
		
	}
	
	
	// 在每个测试方法执行之前执行一次该方法
	// 如果该测试类中有20个测试方法(不含@Ingore的方法),则该方法需要执行20次
	@Before
	public void before() {
		System.out.println("befor");
	}
	
	// 在每个测试方法执行之后执行一次该方法
	// 如果该测试类中有20个测试方法(不含@Ingore的方法),则该方法需要执行20次
	@After
	public void after() {
		System.out.println("after");
	}
	
	
	// 在该类的所有测试方法执行前执行一次
	// 每个测试类中该测试方法仅执行一次,且该方法必须为static类型的
	// 一般用于比较耗时的测试前的工作,比如建立数据库连接,读取配置文件等
	@BeforeClass
	public static void beforeClass() {
		System.out.println("beforeClass");
	}
	
	// 在该类的所有测试方法执行结束后执行一次
	// 每个测试类中该测试方法仅执行一次,且该方法必须为static类型的
	@AfterClass
	public static void afterClass() {
		System.out.println("afterClass");
	}

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值