JUnit4 笔记
1. JUnit4基础
JUnit4 与 JUnit3不同的是,不需要继承测试类,而是通过JDK5提供的注解去标识测试方法。
常用的注解如下:
@Before:初始化方法 对于每一个测试方法都要执行一次(注意与BeforeClass区别,后者是对于所有方法执行一次)
@After:释放资源 对于每一个测试方法都要执行一次(注意与AfterClass区别,后者是对于所有方法执行一次)
@Test:测试方法,在这里可以测试期望异常和超时时间
--@Test(expected=ArithmeticException.class)检查被测方法是否抛出ArithmeticException异常
--@Test(timeout=100)检查测试方法运行耗费的时间,如果超时,则抛出异常:test timed out after xxx milliseconds
@Ignore:忽略的测试方法
@BeforeClass:针对所有测试,只执行一次,且必须为static void
@AfterClass:针对所有测试,只执行一次,且必须为static void
一个JUnit4的单元测试用例执行顺序为:
@BeforeClass -> @Before -> @Test -> @After -> @AfterClass;
一个Junit4的单元测试方法执行顺序为:
@Before -> @Test -> @After
示例代码:
package com.khlin.test.junit.jmockit.demo; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; public class CameraTest { private static Camera camera; //针对所有测试,只运行一次。且只能是static方法
@BeforeClass
public static void init() {
camera = new Camera();
System.out.println("annotation BeforeClass:");
} //每个测试方法调用前都会调用此方法
@Before
public void initClass() {
System.out.println("annotation Before:");
} @Test(timeout=1)
public void testTakePhoto() {
Assert.assertEquals(48, camera.takePhoto("abc"));
System.out.println("test method");
} @After
public void teardownClass() {
System.out.println("annotation After:");
} @AfterClass
public static void teardown() {
System.out.println("annotation AfterClass:");
}
}
2.JUnit4高级特性
此外,JUnit4为单元测试提供了默认的测试运行器,它的测试方法都是由它负责执行的。
开发者也可以通过继承org.junit.runner.Runner来定制自己的运行器,然后通过RunWith注解指定对应的测试运行器。
作为非专业测试人员,一般的默认运行器就可以满足需要了。当要使用一些高级特性时,就必须显式指定测试运行器。
高级特性常用的有参数化和套件测试。
1)参数化测试
构建无限组数据,对一个方法进行测试,优点是多次测试时测试方法只需要写一个,缺点是这样只能对一个方法进行测试,除非有必要大量测试,否则意义不是很大。
步骤如下:
1.在测试类上添加注解@RunWith(Parameterized.class),指定测试运行器
2.创建用于存储期望值和参数的变量
3.声明构造方法,为上述变量赋值
4.声明数据供给方法,static,返回值为Collection类型(二维数组),用@Parameters注解
示例代码:
package com.khlin.test.junit.jmockit.demo; import java.util.Arrays;
import java.util.Collection; import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; //0.注解,表示是参数化测试
@RunWith(Parameterized.class)
public class CalculatorTest { private Calculator calculator; //1.创建用于存储期望值和参数 private int expected; private int i; private int j; //2. 构造方法赋值
public CalculatorTest(int expected, int i, int j) {
// TODO Auto-generated constructor stub
this.expected = expected;
this.i = i;
this.j = j;
} //3. 数据供给方法,static,返回值为Collection类型,用@Parameters注解
@Parameters
public static Collection initParameter() {
return Arrays.asList(new Object[][] { { 2, 1, 1 }, { 3, 2, 1 },
{ 88, 22, 66 }});
} // 每次方法前都会调用
@Before
public void init() {
calculator = new Calculator();
} @Test
public void testAdd() {
Assert.assertEquals(expected, calculator.add(i, j));
} @After
public void teardown() {
this.calculator = null;
}
}
2)套件测试
实际上就是将所有的测试类,集合到一个总的测试类当中,运行时将会依次去调用所有测试类的测试方法,意义。。。。好像也不是很大的样子。
步骤如下:
1.创建一个空的测试类
2.在该测试类上使用注解,@RunWith(Suite.class)指定运行器,@SuiteClasses({xxx.class, xxx.class})指定集合进来的测试类
3.保证这个空类使用public 修饰,而且存在公开的不带有任何参数的构造函数
以上面两段代码为例,套件测试类的代码如下:
package com.khlin.test.junit.jmockit.demo; import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class)
@SuiteClasses({CalculatorTest.class, CameraTest.class})
public class SuitTest { }
运行结果,依次调用了测试方法:
JUnit4 笔记的更多相关文章
- junit4笔记
这两天在复习hibernate,看的小峰的视频,觉得很不错. 现在把里面的junit4的一些使用方法记下来.方便以后的差用.代码如下. package com.java1234.service; im ...
- Junit4学习笔记
一.初始化标注 在老Junit4提供了setUp()和tearDown(),在每个测试函数调用之前/后都会调用. @Before: Method annotated with @Before exec ...
- Java 学习笔记 Junit4单元测试使用
Junit使用 1.导入Junit包 到官网下载个Junit4.12.jar文件,放在lib目录 或者在类的空白处打@Test,之后按下alt+enter,选择添加Junit4依赖 之后就会弹出一个窗 ...
- JUnit4 学习笔记
一.环境搭建: 1.需要用的包: JUnit4.7:http://files.cnblogs.com/files/ShawnYang/junit4.7.zip hamcrest-1.2:http:// ...
- Junit4学习笔记--方法的执行顺序
package com.lt.Demo.TestDemo; import java.util.Arrays; import java.util.Collection; import org.junit ...
- JUnit4源码学习笔记
先上一个在Spring-test下运行的调用栈 自底向上: JUnitStarter IDEA对JUnit的支持,调用JUnitCore.run(Runner),将注解@RunWith指定的Runne ...
- JUnit4 入门笔记
Test注解的两个可选参数 expected timeout The Test annotation supports two optional parameters. The first, expe ...
- ActiveMQ学习笔记(5)——使用Spring JMS收发消息
摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...
- Robotium编写测试用例如何模拟Junit4的BeforeClass和AfterClass方法2 - SingleLaunchActivityTestCase
本文来源于:http://blog.csdn.net/zhubaitian/article/details/39296753 在上一遍笔记博客中本以为只能在Setup和TearDown中做条件判断来实 ...
随机推荐
- 【 D3.js 选择集与数据详解 — 4 】 enter和exit的处理方法以及处理模板
绑定数据之后,选择集分为三部分:update.enter.exit.这三部分的处理办法是什么呢?本文将讲解其处理方法,以及一个常用的处理模板. 1. enter的处理方法 如果没有足够的元素,那么处理 ...
- c语言字符类别测试库函数#include<ctype.h>
字符类测试<ctype.h> 头文件<ctype.h>中说明了一些用于测试字符的函数.每个函数的变量均为int类型,变量的值必须是EOF或可用unsigned char类型表示 ...
- JavaScript window.location对象
JavaScript window.location对象 示例 注意 方法 经常使用window.location,它的结构总是记不住,简单梳理下,方便以后查询. 示例 URL:http://b. ...
- (转载)PHP的内存限制 Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
(转载)http://blog.csdn.net/beyondlpf/article/details/7794028 Fatal error: Allowed memory size of 13421 ...
- maven,spring,mybatis集成错误
maven,spring,mybatis集成的时候单元测试junit测试没问题,但mvn jetty:run 就报错误 错误: org.apache.ibatis.binding.BindingExc ...
- linux 内存管理——内核的shmall 和shmmax 参数
内核的 shmall 和 shmmax 参数 SHMMAX= 配置了最大的内存segment的大小 ------>这个设置的比SGA_MAX_SIZE大比较好. SHMMIN= 最小的内存seg ...
- 页面置换算法(最佳置换算法、FIFO置换算法、LRU置换算法、LFU置换算法)
页面置换产生的原因是:分页请求式存储管理(它是实现虚拟存储管理的方法之一,其中一个特性是多次性-->多次将页面换入或换出内存) 效果最好的页面置换算法:最佳置换算法 比较常用的页面置换算法有:F ...
- HDU4417 - Super Mario(主席树)
题目大意 给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 题解 和静态的区间第K大差不多,这题是<=K,先建立好n颗主席树,然后用第R颗主席树区间[1,K]内数的数量减去第L-1 ...
- HDU 1042 N!(高精度计算阶乘)
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 使用 AppFuse 的七个理由
mvn -e archetype:generate -B -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfus ...