I’ve created a custom JUnit 4 runner(extends BlockJUnit4Runner) whose purpose is to run tests which are necessary for other tests to be valid before those tests are run. For example, if you were testing some file IO, the read/write test would require the open/close tests to work properly.
This runs the tests properly with any number of requires and reports back fine for tests that don’t require anything else, but when I run a test which does require another, I get “Done: 1 of 2”, and the event log says “Tests passed: 1 passed”. This is the case even if the requiring test(which ran second) failed.
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
if (!alreadyRunMethods.containsKey(method.getName())) {
boolean requiredMethodsPassed = true;
for (Annotation anno : method.getAnnotations()) {
if (anno instanceof Requires) {
requiredMethodsPassed = runRequiredMethods((Requires) anno, notifier, method);
break;
}
}
if (requiredMethodsPassed) {
super.runChild(method, notifier);
}else{
notifier.fireTestAssumptionFailed(new Failure(describeChild(method), new AssumptionViolatedException("Required methods failed.")));
}
}
}
private boolean runRequiredMethods(Requires req, RunNotifier notifier, FrameworkMethod parentMethod) {
boolean passed = true;
for (String methodName : req.value()) {
FrameworkMethod method = methodByName.get(methodName);
if (method == null) {
throw new RuntimeException(String.format("Required test method '%s' on test method '%s' does not exist.", methodName, parentMethod.getName()));
}
runChild(method, notifier);
Boolean methodPassed = alreadyRunMethods.get(method.getName());
methodPassed = methodPassed == null ? false : methodPassed;
passed &= methodPassed;
}
return passed;
}
Your custom runner should preperly define
org.junit.runners.ParentRunner#getDescriptionthen IDEA would correctly show the tree.