在Junit中扩展ParentRunner

时间:2016-07-10 11:43:41

标签: java junit junit4 runner

我想从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

1 个答案:

答案 0 :(得分: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