Java 错误的junit测试名称
我试图编写自己的junit runner,但目前我仍无法返回正确的测试描述Java 错误的junit测试名称,java,junit,junit4,Java,Junit,Junit4,我试图编写自己的junit runner,但目前我仍无法返回正确的测试描述 public class ParameterizedWrapper extends Suite { private List<Runner> fRunners; /** * @throws Throwable * */ public ParameterizedWrapper(final Class<?> clazz) throws Thr
public class ParameterizedWrapper extends Suite {
private List<Runner> fRunners;
/**
* @throws Throwable
*
*/
public ParameterizedWrapper(final Class<?> clazz) throws Throwable {
super(clazz, Collections.<Runner>emptyList());
fRunners = constructRunners(getParametersMethod());
}
protected List<Runner> constructRunners(final FrameworkMethod method) throws Exception, Throwable {
@SuppressWarnings("unchecked")
Iterable<Object[]> parameters = (Iterable<Object[]>) getParametersMethod().invokeExplosively(null);
ArrayList<Runner> runners = new ArrayList<Runner>();
int index = 0;
for (Object[] parameter : parameters) {
Class<?> testClass = getTestClass().getJavaClass();
WrappedRunner wrappedRunner = testClass.getAnnotation(WrappedRunner.class);
Runner runner = wrappedRunner.value().getConstructor(Class.class).newInstance(getTestClass().getJavaClass());
runners.add(new WrappingRunner(runner, parameter, testClass, index++));
}
return runners;
}
private FrameworkMethod getParametersMethod() throws Exception {
List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(Parameters.class);
for (FrameworkMethod each : methods) {
if (each.isStatic() && each.isPublic()) {
return each;
}
}
throw new Exception("No public static parameters method on class " + getTestClass().getName());
}
@Override
protected List<Runner> getChildren() {
return fRunners;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public static @interface WrappedRunner {
Class<? extends Runner> value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public static @interface ParameterSetter {
}
}
class WrappingRunner extends Runner {
private Runner wrappedRunner;
private Object[] parameters;
private Class<?> testClass;
private int testPosition;
public WrappingRunner(final Runner runner, final Object[] params, final Class<?> clazz, final int position) {
wrappedRunner = runner;
parameters = params;
testClass = clazz;
testPosition = position;
}
@Override
public Description getDescription() {
Description originalDescription = wrappedRunner.getDescription();
Description newDescription = Description.createSuiteDescription(nameFor(""), new Annotation[0]);
for (Description child : originalDescription.getChildren()) {
newDescription.addChild(decorateChildDescription(child));
}
return newDescription;
}
private String nameFor(String name) {
return String.format("%1$s[%2$s]", name, testPosition);
}
protected Description decorateChildDescription(final Description originalChildDescription) {
Description d = Description.createTestDescription(originalChildDescription.getTestClass(),
nameFor(originalChildDescription.getMethodName()),
originalChildDescription.getAnnotations().toArray(new Annotation[0]));
return d;
}
@Override
public void run(final RunNotifier notifier) {
try {
ParameterStorage.storeParameters(testClass, parameters);
wrappedRunner.run(notifier);
} finally {
ParameterStorage.clearParameters(testClass);
}
}
}
公共类ParameterizedWrapper扩展套件{
私人名单;
/**
*@可丢弃
*
*/
公共参数化包装器(最终类clazz)抛出可丢弃的{
super(clazz,Collections.emptyList());
fRunners=constructRunners(getParametersMethod());
}
受保护列表构造函数运行程序(最终FrameworkMethod方法)引发异常,可丢弃{
@抑制警告(“未选中”)
Iterable参数=(Iterable)getParametersMethod().InvokeeExplosive(null);
ArrayList runners=新的ArrayList();
int指数=0;
for(对象[]参数:参数){
类testClass=getTestClass().getJavaClass();
WrappedRunner-WrappedRunner=testClass.getAnnotation(WrappedRunner.class);
Runner Runner=wrappedRunner.value().getConstructor(Class.Class).newInstance(getTestClass().getJavaClass());
添加(新的包装器(runner、参数、testClass、index++);
}
复赛选手;
}
私有FrameworkMethod getParametersMethod()引发异常{
List methods=getTestClass().getAnnotatedMethods(Parameters.class);
for(每个框架方法:方法){
if(each.isStatic()&&each.isPublic()){
返回每个;
}
}
抛出新异常(“类上没有公共静态参数方法”+getTestClass().getName());
}
@凌驾
受保护列表getChildren(){
返回fRunners;
}
@保留(RetentionPolicy.RUNTIME)
@目标({ElementType.TYPE})
公共静态@interface WrappedRunner{
类testClass;
私人int测试位置;
公共包装器(最终运行器,最终对象[]参数,最终类clazz,最终int位置){
wrappedRunner=跑步者;
参数=参数;
testClass=clazz;
testPosition=位置;
}
@凌驾
公共描述getDescription(){
Description originalDescription=wrappedRunner.getDescription();
Description newDescription=Description.createSuiteDescription(nameFor(“”),新注释[0]);
对于(子描述:originalDescription.getChildren()){
newDescription.addChild(decorateChildDescription(child));
}
返回newDescription;
}
专用字符串名称(字符串名称){
返回字符串。格式(“%1$s[%2$s]”,名称,testPosition);
}
受保护的描述decorateChildDescription(最终描述原始ChildDescription){
Description d=Description.createTestDescription(原始ChildDescription.getTestClass(),
nameFor(originalChildDescription.getMethodName()),
originalChildDescription.getAnnotations().toArray(新注释[0]);
返回d;
}
@凌驾
公共无效运行(最终运行通知程序通知程序){
试一试{
参数storage.storeParameters(testClass,参数);
wrappedRunner.run(通知程序);
}最后{
clearParameters(testClass);
}
}
}
我有一些测试类来检查runner是否工作。runner工作正常,但测试命名奇怪。在eclipse中,它显示所有测试,并添加无根测试类别
surefire根本不使用我的命名:
我比较了在我的runner中生成的描述对象和在
参数化的runner中生成的描述对象,似乎没有什么区别。我检查了更多,发现由我的runner生成的描述是可以的。但这并不重要,因为它与实际测试执行期间使用的描述不一致。这就是eclipse显示ent的原因这就是为什么surefire没有显示我的名字
目前,我想使用我自己的通知程序来捕获测试开始点,并在该点替换配置
如果有人有更好的解决方案,我想知道:)。这有点难看,但将子运行程序列表传递给父构造函数更安全:
public ParameterizedWrapper(final Class<?> clazz) throws Throwable {
super(clazz, constructRunners(getParametersMethod());
}
private static List<Runner> constructRunners(final FrameworkMethod method)
throws Throwable {
...
公共参数化包装器(最终类clazz)抛出可丢弃的{
super(clazz,constructRunners(getParametersMethod());
}
私有静态列表构造函数(最终框架方法)
扔掉的{
...
您不需要重写Suite.getChildren()
嗯,我从junit Parameterized runner那里得到了一个想法和一些代码