簡體   English   中英

在Junit中擴展ParentRunner

[英]Extending ParentRunner in Junit

我想從ParentRunner擴展測試。 我這樣做是為了學習,而不是任何特定的場景。 我有下面和下面的類,也有輸出。 我不明白一些事情:1。為什么“describeChild”被反復召喚3次? 2.為什么測試沒有運行?(“doOne”和“doTwo”)? 取消重新排列此行://結果result = JUnitCore.runClasses(arg0.getClass()); 正在執行測試,但我不明白為什么它會這樣工作? 3.最重要的是 - 那條線:@SuiteClasses({ChildOne.class,ChildTwo.class})? 它對代碼的行為沒有任何影響......非常感謝任何響應的人:-)

套房類:

@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

我可以回答一些問題。

關於@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM