2016-07-10 55 views
1

我想運行從ParentRunner擴展的測試。 我正在學習,而不是任何特定的場景。 我有下面和下面的類也有輸出。 我不明白幾件事: 1.爲什麼「describeChild」重複調用3次? 2.爲什麼測試沒有運行?(「doOne」和「doTwo」)?不知道這一行: // Result result = JUnitCore.runClasses(arg0.getClass()); 正在執行測試,但我不明白爲什麼它會這樣工作? 3.最重要的是 - 那條線: @SuiteClasses({ChildOne.class,ChildTwo.class})? 這對代碼的行爲沒有影響...... 非常感謝任何人誰迴應:-)在Junit中擴展ParentRunner

套房類:

@RunWith(FamilyRunner.class) 
@SuiteClasses({ChildOne.class, ChildTwo.class, ChildThree.class}) 
public class Suite { 
//nothing here 
} 

亞軍類:

public class FamilyRunner extends ParentRunner<ParentClass>{ 

public FamilyRunner(Class<?> klass) throws InitializationError { 
     super(klass); 
} 


@Rule 
public TestName name = new TestName(); 

@Override 
protected List<ParentClass> getChildren() { 

    List<ParentClass> list = new ArrayList<>(); 
    System.out.println("Adding items to list"); 
    list.add(new ChildOne()); 
    list.add(new ChildTwo()); 

    return list; 
} 


@Override 
protected Description describeChild(ParentClass arg0) { 
    System.out.println("describeChild class: " + arg0.getClass().getSimpleName()); 
    Description desc = Description.createTestDescription(name.getMethodName(), 
      name.getMethodName(), getClass().getAnnotations()); 

    return desc; 
} 

@Override 
protected void runChild(ParentClass arg0, RunNotifier arg1) { 
    System.out.println("runChild " + arg0.getClass().getSimpleName()); 
    //Result result = JUnitCore.runClasses(arg0.getClass()); 
} 
} 

和:

public class ParentClass { 

    public ParentClass() { 
     System.out.println("created parent class"); 
    } 
} 

public class ChildOne extends ParentClass { 
    public ChildOne() { 
     System.out.println("created ChildOne class"); 
    } 

    @Test 
    public void doOne(){ 
     System.out.println("doOne"); 
    } 
} 

public class ChildTwo extends ParentClass { 
    public ChildTwo() { 
     System.out.println("created ChildTwo class"); 
    } 

    @Test 
    public void doTwo(){ 
     System.out.println("doTwo"); 
    } 
} 

控制檯打印:

Adding items to list 
created parent class 
created ChildOne class 
created parent class 
created ChildTwo class 
describeChild class: ChildOne 
describeChild class: ChildTwo 
describeChild class: ChildOne 
describeChild class: ChildTwo 
describeChild class: ChildOne 
describeChild class: ChildTwo 
runChild ChildOne 
runChild ChildTwo 

回答

0

我可以回答一些問題。

關於@SuiteClasses({ChildOne.class,ChildTwo.class}),您並未使用此註釋值構建列表。

你可以這樣做:

protected List<ParentClass> getChildren() { 
    Annotation[] runnerAnnotations = super.getRunnerAnnotations(); 
    System.out.println(runnerAnnotations); 

    Optional<Annotation> suitClass = Arrays.stream(runnerAnnotations) 
      .filter(a -> a.annotationType().equals(SuiteClasses.class)) 
      .findFirst(); 

    List<ParentClass> list = new ArrayList<>(); 
    if (suitClass.isPresent()) { 
     SuiteClasses s = (SuiteClasses) suitClass.get(); 
     s.value(); 
     System.out.println("Adding items to list"); 
     for (Class<?> c : s.value()) { 
      Class<ParentClass> cp = (Class<ParentClass>) c; 
      try { 
       list.add(cp.newInstance()); 
      } catch (InstantiationException | IllegalAccessException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    return list; 
} 

我認爲你可以找到org.junit.runners.Suite源的詳細信息:http://grepcode.com/file/repo1.maven.org/maven2/junit/junit/4.8.1/org/junit/runners/Suite.java