今天在用Junit进行单元测试的时候,遇到一个诡异的问题。测试代码如下:
Java类:Calculate.java
public class Calculate {
public int sum(int var1, int var2) {
System.out.println("Adding values " + var1 + " + " + var2);
return var1 + var2;
}
public int subtract(int var1,int var2){
System.out.println("Subtract values "+ var1+" - "+ var2);
return var1-var2;
}
}
测试类:ParameterizedCalculateTest.java
@RunWith(Parameterized.class)
public class ParameterizedCalculateTest {
private int expected;
private int var1;
private int var2;
public ParameterizedCalculateTest(int expected, int var1, int var2) {
this.expected = expected;
this.var1 = var1;
this.var2 = var2;
}
@Parameters
public static Collection prepareParamters() {
return Arrays.asList(new Integer[][] { { 3, 1, 2 }, { 5, 2, 3 },
{ 7, 3, 4 }, { 9, 4, 5 } });
}
@Test
public void testSum() {
System.out.println("Additions with parameters,first:" + var1 + " and second:" + var2);
Calculate cal = new Calculate();
Assert.assertEquals(expected, cal.sum(var1, var2));
}
}
错误堆栈如下:
java.lang.Exception: No tests found matching Method testSum(com.milan.gjunit.ParameterizedCalculateTest) from org.junit.internal.requests.ClassRequest@739495b8
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestMethodReference.<init>(JUnit4TestMethodReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:54)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
刚开始看到异常信息:"java.lang.Exception: No tests found matching Method testSum",猜测难道测试类的方法名要和对应java类中的方法名一样么?试下呗,于是将测试类改为:
@RunWith(Parameterized.class)
public class ParameterizedCalculateTest {
private int expected;
private int var1;
private int var2;
public ParameterizedCalculateTest(int expected, int var1, int var2) {
this.expected = expected;
this.var1 = var1;
this.var2 = var2;
}
@Parameters
public static Collection prepareParamters() {
return Arrays.asList(new Integer[][] { { 3, 1, 2 }, { 5, 2, 3 },
{ 7, 3, 4 }, { 9, 4, 5 } });
}
@Test
public void sum() {
System.out.println("Additions with parameters,first:" + var1 + " and second:" + var2);
Calculate cal = new Calculate();
Assert.assertEquals(expected, cal.sum(var1, var2));
}
}
结果还是不行。。。
果断搜索之,搜到stackoverflow上有类似的问题:
http://stackoverflow.com/questions/12798079/initializationerror-with-eclipse-and-junit4-when-executing-a-single-test/18438718#18438718。
于是乎抱着试试看的想法,扩展Parameterized.Java如下:
public class IDECompatibleParameterized extends Parameterized {
public IDECompatibleParameterized(Class<?> klass) throws Throwable {
super(klass);
}
public void filter(Filter filter) throws NoTestsRemainException {
super.filter(new FilterDecorator(filter));
}
/**
* Running single test in case of parameterized test causes issue as explained in
* http://youtrack.jetbrains.com/issue/IDEA-65966
*
* As a workaround we wrap the original filter and then pass it a wrapped description
* which removes the parameter part (See deparametrizedName)
*/
private static class FilterDecorator extends Filter {
private final Filter delegate;
private FilterDecorator(Filter delegate) {
this.delegate = delegate;
}
@Override
public boolean shouldRun(Description description) {
return delegate.shouldRun(wrap(description));
}
@Override
public String describe() {
return delegate.describe();
}
}
private static Description wrap(Description description) {
String name = description.getDisplayName();
String fixedName = deparametrizedName(name);
Description clonedDescription =
Description.createSuiteDescription(fixedName,description.getAnnotations().toArray(new Annotation[0]));
for(Description child : description.getChildren()){
clonedDescription.addChild(wrap(child));
}
return clonedDescription;
}
private static String deparametrizedName(String name) {
//Each parameter is named as [0], [1] etc
if(name.startsWith("[")){
return name;
}
//Convert methodName[index](className) to
//methodName(className)
int indexOfOpenBracket = name.indexOf('[');
int indexOfCloseBracket = name.indexOf(']')+1;
return name.substring(0,indexOfOpenBracket).concat(name.substring(indexOfCloseBracket));
}
}
PS:装饰器模式用的很赞。。。
然后再把测试类改为:
@RunWith(IDECompatibleParameterized.class)
public class ParameterizedCalculateTest {
private int expected;
private int var1;
private int var2;
public ParameterizedCalculateTest(int expected, int var1, int var2) {
this.expected = expected;
this.var1 = var1;
this.var2 = var2;
}
@Parameters
public static Collection prepareParamters() {
return Arrays.asList(new Integer[][] { { 3, 1, 2 }, { 5, 2, 3 },
{ 7, 3, 4 }, { 9, 4, 5 } });
}
@Test
public void sum() {
System.out.println("Additions with parameters,first:" + var1 + " and second:" + var2);
Calculate cal = new Calculate();
Assert.assertEquals(expected, cal.sum(var1, var2));
}
}
修改点为:
@RunWith(IDECompatibleParameterized.class)
果断Pass。
记录下,顺便对国际友人表示感谢,希望对遇到类似问题的朋友有所帮助。
分享到:
相关推荐
junit参数化测试的使用方法,数据驱动:数组,excel
Junit参数化测试的使用方法博客 的源代码
有关于java语言的单元测试工具,想研究junit的可以看看
单元测试之道(Java):使用JUnit进行单元测试。单元测试是提高代码质量的有效手段,但大部分开发人员由于种种原因都不乐意进行单元测试。
Unit是一个Java语言的单元...Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。
Junit单元测试Junit单元测试Junit单元测试Junit单元测试
eclipse下利用ant、junit进行自动化测试例子源码
junit测试测试junit测试测试junit测试测试junit测试测试
junit3 junit4 api,单元测试的利器
1.seleRrj是工程包,包含简单的自动化测试demo以及jar包,已经加入junit4支持;调试运行通过; 2.selenium-fireFox插件,安装在火狐浏览器里面 用于录制自动化脚本和学习自动化测试; 3.我用的火狐版本是v21.0版本,...
junit测试 test txt file
JUNIT多线程测试
Junit单元测试Junit单元测试Junit单元测试Junit单元测试
JUnit4 报错 no tests found with test runner 'JUnit4' 需要添加 hamcrest-core-2.2.jar 包重新启动
用Junit进行单元测试
JUNIT软件测试软件测试技术JUnit和单元测试入门简介软件测试1、几个相关的概念白盒测试——把测试对象看作一个打开的盒子,程序内部的逻辑结构和其他信息对测试人员是公开的。回归测试——软件或环境的修复或更正后...
简单的ATM的代码测试技术,基于Java的代码测试,里面实现了Server类和测试类,需要添加Junit和Elcmma
★测试夹具:(Test Fixture):就是测试运行程序会在测试方法之前自动初始化、回收资源的工作。 Junit4引入的是类级别的夹具,编写规范如下: ①创建测试用例的实例 ②使用注解BeforeClass修饰用于初始化夹具的方法 ...
通过单元测试工具(Junit)支持,使学生对单元测试中测试用例的设计原则,设计方法进行了解和掌握。
junit3单元自动化测试详细解释,包括断言等基本知识。