作者:臭小子 | 2023-09-01 17:17
当你运行JUnit 4 ParameterizedTest与Eclipse的TestRunner,图形表示是相当愚蠢的:每个测试你有一个叫做节点[0]
,[1]
等等是否有可能给测试[0]
,[1]
等明确的名字呢?实现toString
测试方法似乎没有帮助.
(这是JUnit测试的后续问题,具有动态的测试次数.)
1> bruno conde..: 我认为jUnit 4中没有内置 任何内容来实现这一点.
我已经实施了一个解决方案.我Parameterized
基于现有的类构建了自己的类:
public class MyParameterized extends TestClassRunner {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Parameters {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Name {
}
public static Collection eachOne(Object... params) {
List results = new ArrayList();
for (Object param : params)
results.add(new Object[] { param });
return results;
}
// TODO: single-class this extension
private static class TestClassRunnerForParameters extends TestClassMethodsRunner {
private final Object[] fParameters;
private final Class> fTestClass;
private Object instance;
private final int fParameterSetNumber;
private final Constructor> fConstructor;
private TestClassRunnerForParameters(Class> klass, Object[] parameters, int i) throws Exception {
super(klass);
fTestClass = klass;
fParameters = parameters;
fParameterSetNumber = i;
fConstructor = getOnlyConstructor();
instance = fConstructor.newInstance(fParameters);
}
@Override
protected Object createTest() throws Exception {
return instance;
}
@Override
protected String getName() {
String name = null;
try {
Method m = getNameMethod();
if (m != null)
name = (String) m.invoke(instance);
} catch (Exception e) {
}
return String.format("[%s]", (name == null ? fParameterSetNumber : name));
}
@Override
protected String testName(final Method method) {
String name = null;
try {
Method m = getNameMethod();
if (m != null)
name = (String) m.invoke(instance);
} catch (Exception e) {
}
return String.format("%s[%s]", method.getName(), (name == null ? fParameterSetNumber : name));
}
private Constructor> getOnlyConstructor() {
Constructor>[] constructors = getTestClass().getConstructors();
assertEquals(1, constructors.length);
return constructors[0];
}
private Method getNameMethod() throws Exception {
for (Method each : fTestClass.getMethods()) {
if (Modifier.isPublic((each.getModifiers()))) {
Annotation[] annotations = each.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() == Name.class) {
if (each.getReturnType().equals(String.class))
return each;
else
throw new Exception("Name annotated method doesn't return an object of type String.");
}
}
}
}
return null;
}
}
// TODO: I think this now eagerly reads parameters, which was never the
// point.
public static class RunAllParameterMethods extends CompositeRunner {
private final Class> fKlass;
public RunAllParameterMethods(Class> klass) throws Exception {
super(klass.getName());
fKlass = klass;
int i = 0;
for (final Object each : getParametersList()) {
if (each instanceof Object[])
super.add(new TestClassRunnerForParameters(klass, (Object[]) each, i++));
else
throw new Exception(String.format("%s.%s() must return a Collection of arrays.", fKlass.getName(), getParametersMethod().getName()));
}
}
private Collection> getParametersList() throws IllegalAccessException, InvocationTargetException, Exception {
return (Collection>) getParametersMethod().invoke(null);
}
private Method getParametersMethod() throws Exception {
for (Method each : fKlass.getMethods()) {
if (Modifier.isStatic(each.getModifiers())) {
Annotation[] annotations = each.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() == Parameters.class)
return each;
}
}
}
throw new Exception("No public static parameters method on class " + getName());
}
}
public MyParameterized(final Class> klass) throws Exception {
super(klass, new RunAllParameterMethods(klass));
}
@Override
protected void validate(MethodValidator methodValidator) {
methodValidator.validateStaticMethods();
methodValidator.validateInstanceMethods();
}
}
要像以下一样使用:
@RunWith(MyParameterized.class)
public class ParameterizedTest {
private File file;
public ParameterizedTest(File file) {
this.file = file;
}
@Test
public void test1() throws Exception {}
@Test
public void test2() throws Exception {}
@Name
public String getName() {
return "coolFile:" + file.getName();
}
@Parameters
public static Collection data() {
// load the files as you want
Object[] fileArg1 = new Object[] { new File("path1") };
Object[] fileArg2 = new Object[] { new File("path2") };
Collection data = new ArrayList();
data.add(fileArg1);
data.add(fileArg2);
return data;
}
}
这意味着我先前实例化了测试类.我希望这不会导致任何错误...我想我应该测试测试:)