2016-07-10 13 views
1

ParentRunner부터 연장하여 테스트를하고 싶습니다. 특정 시나리오가 아니라 학습을 위해이 작업을하고 있습니다. 아래 출력 클래스도 있습니다. 몇 가지를 이해하지 못합니다. 1. "describeChild"가 3 회 반복 호출되는 이유는 무엇입니까? 2. 테스트가 실행되지 않은 이유는 무엇입니까 ("doOne"및 "doTwo")? 이 줄의 주석 처리를 제거하십시오 : // 결과 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

몇 가지 질문에 대답 할 수 있습니다.

About @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