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
I can answer some questions.
About @SuiteClasses({ChildOne.class, ChildTwo.class}) you aren't building the list using this annotation values.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.