Java 在Junit中扩展ParentRunner
我想使用扩展自Java 在Junit中扩展ParentRunner,java,junit,junit4,runner,Java,Junit,Junit4,Runner,我想使用扩展自ParentRunner运行测试。 我这样做是为了学习,不是为了任何特定的场景。 我有下面的类,下面还有输出。 有几件事我不明白: 1.为什么“describeChild”被反复调用三次? 2.为什么测试没有运行?(“doOne”和“doTwo”)? 取消对此行的注释: //结果=JUnitCore.runClasses(arg0.getClass()); 正在执行测试,但我不明白为什么会这样? 3.最重要的是,那一行呢: @SuiteClasses({ChildOne.class
ParentRunner
运行测试。
我这样做是为了学习,不是为了任何特定的场景。
我有下面的类,下面还有输出。
有几件事我不明白:
1.为什么“describeChild”被反复调用三次?
2.为什么测试没有运行?(“doOne”和“doTwo”)?
取消对此行的注释:
//结果=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());
}
}
控制台打印:
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
我可以回答一些问题 关于@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;
}
受保护的列表getChildren(){
注释[]runnerAnnotations=super.getRunnerAnnotations();
System.out.println(运行说明);
可选的我可以回答一些问题
关于@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;
}
受保护的列表getChildren(){
注释[]runnerAnnotations=super.getRunnerAnnotations();
System.out.println(运行说明);
可选的