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");
}
}