JUnit4参数的使用
用JUnit4进行参数化测试
参数化测试是一个JUnit 3不具备的功能。
基本使用方法
@RunWith
当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解所指明的运行器(runner)来运行测试,而不是JUnit默认的运行器。
要进行参数化测试,需要在类上面指定如下的运行器:
@RunWith (Parameterized.class)
然后,在提供数据的方法上加上一个@Parameters注解,这个方法必须是静态static的,并且返回一个集合Collection。
在测试类的构造方法中为各个参数赋值,(构造方法是由JUnit调用的),最后编写测试类,它会根据参数的组数来运行测试多次。
文档中的例子
Class Parameterized的文档中有一个例子:
For example, to test a Fibonacci function, write:
@RunWith(Parameterized.class)
public class FibonacciTest
{
@Parameters
public static Collection data()
{
return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
{ 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
}
private int fInput;
private int fExpected;
public FibonacciTest(int input, int expected)
{
fInput = input;
fExpected = expected;
}
@Test
public void test()
{
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
参数化测试实例
还是以前面的Calculator类作为例子,进行参数化测试:
Calculator
package com.mengdd.junit4;
public class Calculator
{
public int add(int a, int b)
{
return a + b;
}
public int subtract(int a, int b)
{
return a - b;
}
public int multiply(int a, int b)
{
return a * b;
}
public int divide(int a, int b) throws Exception
{
if (0 == b)
{
throw new Exception("除数不能为0");
}
return a / b;
}
}
参数化测试加法的类:
package com.mengdd.junit4;
import java.util.Arrays;
import java.util.Collection;
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;
@RunWith(Parameterized.class)
// 指定运行器runner:使用参数化运行器来运行
public class ParametersTest
{
private int expected;// 期待的结果值
private int input1;// 参数1
private int input2;// 参数2
private Calculator calculator = null;
@Parameters
public static Collection prepareData()
{
// 测试数据
Object[][] objects = { { 3, 1, 2 }, { -4, -1, -3 }, { 5, 2, 3 },
{ 4, -4, 8 } };
return Arrays.asList(objects);// 将数组转换成集合返回
}
@Before
public void setUp()
{
calculator = new Calculator();
}
public ParametersTest(int expected, int input1, int input2)
{
// 构造方法
// JUnit会使用准备的测试数据传给构造函数
this.expected = expected;
this.input1 = input1;
this.input2 = input2;
}
@Test
public void testAdd()
{
Assert.assertEquals(this.expected,
calculator.add(this.input1, this.input2));
}
}
原址:http://www.cnblogs.com/mengdd/archive/2013/04/13/3019336.html
JUnit4参数的使用的更多相关文章
- [转]在Eclipse中使用JUnit4进行单元测试(中级篇)
我们继续对初级篇中的例子进行分析.初级篇中我们使用Eclipse自动生成了一个测试框架,在这篇文章中,我们来仔细分析一下这个测试框架中的每一个细节,知其然更要知其所以然,才能更加熟练地应用JUnit4 ...
- junit4 assert类中的assert方法总结
junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类. 1.assertTrue/False([String message,]boolean cond ...
- 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标GPS监控平台
开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...
- 在Eclipse中使用JUnit4进行单元测试(高级篇)
通过前2篇文章,您一定对JUnit有了一个基本的了解,下面我们来探讨一下JUnit4中一些高级特性. 一.高级Fixture 上一篇文章中我们介绍了两个Fixture标注,分别是@Before和@Af ...
- 在Eclipse中使用JUnit4进行单元测试(中级篇)
我们继续对初级篇中的例子进行分析.初级篇中我们使用Eclipse自动生成了一个测试框架,在这篇文章中,我们来仔细分析一下这个测试框架中的每一个细节,知其然更要知其所以然,才能更加熟练地应用JUnit4 ...
- Junit4参数化测试实现程序与用例数据分离
http://touchfu.iteye.com/blog/732930 现状:你是不是还在为自己的TestCase代码杂乱无章而苦恼,咎其根本还在于针对不同的用例,输入参数和mock信息的组装全部作 ...
- Junit3与Junit4的区别
Junit4最大的亮点就是引入了注解(annotation),通过解析注解就可以为测试提供相应的信息,抛弃junit3使用命名约束以及反射机制的方法. /** * 被测试类 */ package co ...
- 使用JUnit4测试Spring
测试DAO import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org ...
- Spring 向页面传值以及接受页面传过来的参数的方式
来源于:http://www.cnblogs.com/liuhongfeng/p/4802013.html 一.从页面接收参数 Spring MVC接收请求提交的参数值的几种方法: 使用HttpSer ...
随机推荐
- bootstrap中如何让响应式图片(img-responsive)水平居中
我们在用bootstrap排版内容的时候,有的时候在内容中需要图片水平居中对齐. 一般情况下,我们的图片都使用了 .img-responsive 类来实现响应式图片.如果需要实现响应式图片水平居中,那 ...
- 简单的例子 关于Java内存管理的讲解
我想做的是,逐行读取文件,然后用该行的电影名去获取电影信息.因为源文件较大,readlines()不能完全读取所有电影名,所以我们逐行读取. 就这段代码,我想要在位置二处使用base64,然后结果呢? ...
- spring beans
所 有 使 用 XML 文 件 进 行 配 置 信 息 加 载 的 Spring IoC 容 器 , 包 括 BeanFactory 和ApplicationContext的所有XML相应实现,都使用 ...
- 一些css知识
两个"::"和一个":"在css3中主要用来区分伪类和伪元素. 1.设置 placeholder属性: // firefox input::-moz-place ...
- PHP 文件包含总结 include require 命名空间 autoload spl_autoload_register 读取文件路径
总结: 1. include或require包含其他文件 使用./或者 ../,这里的当前路径和上一层路径,取决于运行脚本的路径,会存在如下问题. 在写PHP程序时,经常要用到include或requ ...
- Linux 批量修改文件名
背景:在研究MP4解码播放的时候音视频字幕的分片命名不符合规范,分片个数太多只能脚本实现. 解决问题类型: 1.将Garfield1HD_261_dan-*.m4s 统一转换为Garfield1HD_ ...
- 使用 Eclipse PhoneGap 构建 Android 应用程序入门
Eclipse 是一种支持多种技术的开源集成开发环境 (IDE),但本文重点介绍 Java 支持,这也是 Android 应用程序的“母语”.Android 是 Google 发布的开源移动操作系统. ...
- 执行最慢的SQL语句
---执行最慢的SQL语句SELECT top 20(total_elapsed_time / execution_count)/1000 N'平均时间ms',total_elapsed_time/1 ...
- 一个简单的游戏开发框架(五.对象Object)
前面提到我们把行为Action从对象Object中分离了出来,用各种不同的行为组合出对象的功能.大家都知道,面向对象的一个类,就是数据和操作的集合.操作(行为)被分离出来了,数据怎么办呢?操作依赖的数 ...
- Common.Logging log4net Common.Logging.Log4Net 配置
1.log4net 单独配置 log4net支持多种格式的日志输出,我这里只配置输出到本地的txt文件这种格式. <log4net> <root> <appender-r ...