I would like to run tests with extending from 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?
2. why the tests didn't run?("doOne", and "doTwo")?
Uncommentiing this line:
//Result 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})?
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