您可以对JUnitCore使用不同的调用并实现您自己的自定义计算机。
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomParameterized {}
像这样:
@CustomParameterized
public class Example {
private String arg;
public Example(String arg){
this.arg = arg;
}
@org.junit.Test
public void test(){
assertEquals("a",arg);
}
}
的JUnitCore调用将变成:如果你的测试类是用自己的注解定制的电脑可产生一个参数亚军
String[] args = new String[]{"a","b"}
Request request = Request.classes(new CustomComputer(args), Example.class);
Result result = new JUnitCore().run(request);
的定制电脑看起来像:
public class CustomComputer extends Computer {
private final List<Object> args;
public CustomComputer(String[] args) {
this.args = Arrays.<Object>asList(args);
}
@Override
protected Runner getRunner(RunnerBuilder builder, final Class<?> testClass) throws Throwable {
if (testClass.isAnnotationPresent(Test.CustomParameterized.class)) {
final BlockJUnit4ClassRunnerWithParametersFactory factory = new BlockJUnit4ClassRunnerWithParametersFactory();
return new CustomParameterizedRunner(testClass, factory);
}
return builder.runnerForClass(testClass);
}
private class CustomParameterizedRunner extends Suite {
private final Class<?> testClass;
private final BlockJUnit4ClassRunnerWithParametersFactory factory;
public CustomParameterizedRunner(Class<?> testClass, BlockJUnit4ClassRunnerWithParametersFactory factory) throws InitializationError {
super(testClass, Collections.EMPTY_LIST);
this.testClass = testClass;
this.factory = factory;
}
@Override
protected List<Runner> getChildren() {
List<Runner> runners = new ArrayList<>();
for (Object arg : args) {
runners.add(runnerFor(arg, testClass, factory));
}
return runners;
}
}
private static Runner runnerFor(Object arg, Class<?> testClass, BlockJUnit4ClassRunnerWithParametersFactory factory) {
ArrayList<Object> parameters = new ArrayList<>(1);
parameters.add(arg);
String name = testClass.getSimpleName() + "[" + arg + "]";
TestWithParameters test = new TestWithParameters(name, new TestClass(testClass), parameters);
try {
return factory.createRunnerForTestWithParameters(test);
} catch (InitializationError initializationError) {
throw new RuntimeException(initializationError);
}
}
}
谢谢罗比,我会检查这个并更新状态 – ANP