简体   繁体   English

在Junit中扩展ParentRunner

[英]Extending ParentRunner in Junit

I would like to run tests with extending from ParentRunner . 我想从ParentRunner扩展测试。 I'm doing that for learning, not any specific scenario. 我这样做是为了学习,而不是任何特定的场景。 I have the classes below and below there's the output as well. 我有下面和下面的类,也有输出。 I don't understand a few things: 1. Why is "describeChild" called 3 times repeatedly? 我不明白一些事情:1。为什么“describeChild”被反复召唤3次? 2. why the tests didn't run?("doOne", and "doTwo")? 2.为什么测试没有运行?(“doOne”和“doTwo”)? Uncommentiing this line: //Result result = JUnitCore.runClasses(arg0.getClass()); 取消重新排列此行://结果result = JUnitCore.runClasses(arg0.getClass()); is executing the tests but I don't understand why does it work that way? 正在执行测试,但我不明白为什么它会这样工作? 3. and above all - what about that line: @SuiteClasses({ChildOne.class, ChildTwo.class})? 3.最重要的是 - 那条线:@SuiteClasses({ChildOne.class,ChildTwo.class})? it had no influence on the behaviour of the code... Many thanks to anybody who responds:-) 它对代码的行为没有任何影响......非常感谢任何响应的人:-)

Suite class: 套房类:

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

Runner class: 跑步者班:

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

and: 和:

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

The console prints: 控制台打印:

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

I can answer some questions. 我可以回答一些问题。

About @SuiteClasses({ChildOne.class, ChildTwo.class}) you aren't building the list using this annotation values. 关于@SuiteClasses({ChildOne.class,ChildTwo.class})您没有使用此注释值构建列表。

You can do this: 你可以这样做:

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

I think you can find more information in org.junit.runners.Suite source: http://grepcode.com/file/repo1.maven.org/maven2/junit/junit/4.8.1/org/junit/runners/Suite.java 我想你可以在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