2013-02-12 60 views
0

我嘗試編寫自己的junit運行程序,目前我被困在返回正確的測試描述。錯誤的junit測試名稱

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); 
     } 
    } 
} 

我有一些測試類來檢查亞軍是否工作。亞軍工作正常,除了測試命名怪異。在Eclipse中它會顯示所有的測試,並增加了無根的測試類別

enter image description here

和神火不使用我的命名都​​:

enter image description here

我相比,我的亞軍和Parameterized生成的描述對象亞軍,似乎沒有區別。

回答

0

我檢查了一下,發現我的跑步者生成的描述沒問題。但這並不重要,因爲它與實際測試執行期間使用的描述不一致。這就是爲什麼eclipse將條目顯示爲未執行的原因,這就是爲什麼surefire不會顯示我的名字。

目前我認爲要使用我自己的通知程序來抓取測試起點並替換當前的配置。

如果有人有更好的解決方案,我想知道它:)。

1

這是一個有點難看,但它的安全子選手的名單傳遞給父類的構造:

public ParameterizedWrapper(final Class<?> clazz) throws Throwable { 
    super(clazz, constructRunners(getParametersMethod()); 
} 

private static List<Runner> constructRunners(final FrameworkMethod method) 
    throws Throwable { 
    ... 

你不應該需要重寫Suite.getChildren()

+0

咦,我把想法和一些代碼從junit Paramterezied亞軍 – 2013-02-14 07:05:06