JUnit 单元测试几个方法的用法注解
2016-02-19 17:39
232 查看
@Test: 将一个 普通的方法修饰成为一个测试方法
@Test(timeout = 3000)
1、
2、测试方法执行时间是否在预期时间内
3、处理运行时异常
@Test(expected=ArithmeticException.class)
public void TestDivide(){
assertEquals(3, new Calculate().divide(9, 0));
}
2秒在3秒内,测试通过
@BeforeClass: 他会在所有的方法运行前被执行,static修饰,测试类被运行就会被加载,在内存中只有一份实例,适合加载配置文件。
@AfterClass 他会在所有方法运行结束后被执行,static修饰,用于资源的清理,如关闭数据库。
@Before:会在每一个测试方法被运行前执行一次
@After 会在每个测试方法被运行后执行一次
@Ignore 所修饰的测试方法会被测试运行器忽略
@RunWith: 可以修改测试运行器 org.junit.runner.Runner(默认运行器)
测试套件就是组织测试类一起运行的
@RunWith(Suite.class)
写一个作为测试套件的入口类,这个类里不包含其他的方法。
更改测试运行器Suite.class.
将要测试的类作为数组传入到Suite.SuiteClasses({测试类.class数组})
eg:@RunWith(Suite.class) @Suite.SuiteClasses({TaskTest1.class,TaskTest2.class,TaskTest3.class})
@Test(timeout = 3000)
1、
终止死循环,当达到设定的值,结束循环
[code]@Test(timeout = 2000) public void TestWhile(){ // try { // Thread.sleep(2000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } while (true) { System.out.println("终止死循环,当达到设定的值,结束循环"); } }
2、测试方法执行时间是否在预期时间内
[code]@Test(timeout = 3000) public void TestWhile(){ try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // while (true) { // System.out.println("终止死循环,当达到设定的值,结束循环"); // } }
3、处理运行时异常
@Test(expected=ArithmeticException.class)
public void TestDivide(){
assertEquals(3, new Calculate().divide(9, 0));
}
2秒在3秒内,测试通过
@BeforeClass: 他会在所有的方法运行前被执行,static修饰,测试类被运行就会被加载,在内存中只有一份实例,适合加载配置文件。
@AfterClass 他会在所有方法运行结束后被执行,static修饰,用于资源的清理,如关闭数据库。
@Before:会在每一个测试方法被运行前执行一次
@After 会在每个测试方法被运行后执行一次
@Ignore 所修饰的测试方法会被测试运行器忽略
@RunWith: 可以修改测试运行器 org.junit.runner.Runner(默认运行器)
测试套件就是组织测试类一起运行的
@RunWith(Suite.class)
[code]@Suite.SuiteClasses({CalculateTest.class,CalculateTest2.class}) public class SuiteTest { }
写一个作为测试套件的入口类,这个类里不包含其他的方法。
更改测试运行器Suite.class.
将要测试的类作为数组传入到Suite.SuiteClasses({测试类.class数组})
eg:@RunWith(Suite.class) @Suite.SuiteClasses({TaskTest1.class,TaskTest2.class,TaskTest3.class})
相关文章推荐
- java方法,构造方法
- 冒泡排序的理解
- AspectJ基础学习之二搭建环境(转载)
- Git Push 避免用户名和密码方法
- cocos2d-js关于对话框Layer的屏幕适配
- MySQL知识(四)——运算符
- 线程池
- 我有一个 APP 创意,如何将其实现?
- .net uploadify 传递参数 多文件上传
- JSPatch-动态更新IOS APP
- PICT使用教程(设计测试用例工具)
- AspectJ基础学习之一简介(转载)
- IDEA报错Target level '1.6' is incompatible with source level '1.7'
- 初识js中的闭包
- 设计模式之备忘录模式
- Web之一 JS
- 15.Transition动画效果私有API说明
- 4-9 统计个位数字
- Python基础学习笔记(二)语法简介
- 一次服务器环境搭建的记录(nginx+php+subversion)