【问题标题】:Extending ParentRunner in Junit在 Junit 中扩展 ParentRunner
【发布时间】:2016-11-12 11:44:08
【问题描述】:

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

【问题讨论】:

    标签: java junit junit4 runner


    【解决方案1】:

    我可以回答一些问题。

    关于 @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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 2020-05-08
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多