2012-10-09 11 views
8

Meine Testklasse Ausführung ist diese:initializationError mit Eclipse und junit4, wenn ein einzelner Test

/** 
    * The Class TestAddSubscriber. 
    */ 
    @RunWith(LabelledParameterized.class) 
    public class TestAddSubscriber extends AbstractTestSubscriber { 

     /** 
     * Instantiates a new test add subscriber. 
     * 
     * @param label 
     *   the label 
     * @param apiKey 
     *   the api key 
     * @param userKey 
     *   the user key 
     * @param customerId 
     *   the customer id 
     */ 
     public TestAddSubscriber(String label, String apiKey, String userKey, 
       int customerId) { 
      super(label, apiKey, userKey, customerId); 
     } 

     /** 
     * @see com.contactlab.api.test.AbstractTest#setUp() 
     */ 
     @Override 
     @Before 
     public void setUp() throws Exception { 
      super.setUp(); 
     } 

     /** 
     * @see com.contactlab.api.test.AbstractTest#tearDown() 
     */ 
     @Override 
     @After 
     public void tearDown() throws Exception { 
      super.tearDown(); 
     } 

     /** 
     * Generated data. 
     * 
     * @return the collection 
     */ 
     @Parameters 
     public static Collection<Object[]> generatedData() { 
      return DataProvider.generatedCorrectSubscriberData(); 
     } 

     /** 
     * Test add subscriber with internal pk id case sensitive. 
     * 
     * @outcome: success 
     * @expected: success 
     * @obtained: success 
     */ 
     @Test 
     public void testAddSubscriberWithInternalPkIdCaseSensitive() { 

    /** this is the test **/ 

     } 

    /** 
    * other tests 
    **/ 
    } 

Wenn ich führen den Test Suite Ich bin nicht Problem, jedoch Wenn Highlight nur von ihnen ein und es laufe ich habe Unrooted Tests -> InitializationError

Die Klasse verwendet Parameterized.

Die Ausnahme ist:

TestAddSubscriber.testAddSubscriberWithInternalPkIdCaseSensitive 
Unrooted Tests 
initializationError(org.junit.runner.manipulation.Filter) 
java.lang.Exception: No tests found matching Method testAddSubscriberWithInternalPkIdCaseSensitive(com.contactlab.api.test.subscriber.TestAddSubscriber) from [email protected] 
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:37) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestMethodReference.<init>(JUnit4TestMethodReference.java:25) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:54) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

Gibt es etwas, was ich falsch mache?

+0

Es funktioniert wie erwartet (Sie können mit der rechten Maustaste + individuelle Tests ausführen) in Juno, mit JUnit 4.11 und @ RunWith (parametrisierte.class). Versionierung ist aber ziemlich von deiner Einrichtung entfernt ...: - / – Markus

Antwort

5

Sie können möglicherweise durch Überschreiben der org.junit.runners.ParentRunner # Filter das Problem umgehen, indem Sie die Parameterized

public class IDECompatibleParameterized extends Parameterized { 

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

Eine andere Möglichkeit, den Parameternamen zu haben, wäre Verlängerung der Methodennamen in Startkonfiguration zu ändern wie testAddSubscriberWithInternalPkIdCaseSensitive[0]