本文参考与
1: https://www.iteye.com/blog/hedajia-2208403
JUNIT参数化测试,遇到的诡异问题
No tests found matching Method 测试方法名(测试类名) from org.junit.internal.request.ClassRequest@ibdecc21
解决方案:
重写 org.junit.runner.Parameterized
代码如下
public class B2bParameterized extends Parameterized {
public B2bParameterized (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));
}
}
测试类:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(B2bParameterized.class)
参数方法
@B2bParameterized.Parameters
在进行JUNIT参数化测试时遇到了错误:No tests found matching 方法名(类名)。参考了iteye博客和StackOverflow上的解答,解决方案是重写org.junit.runner.Parameterized,并使用PowerMockRunner Delegate搭配自定义的B2bParameterized类。

被折叠的 条评论
为什么被折叠?



