While working with JUnit, it is good to have the idea about the JUnit runners around, as we use one or the other such runners.
So good to know that one can write such Runners for the custom usage.
Try to google around this & check. -
https://www.baeldung.com/junit-4-custom-runners
https://putridparrot.com/blog/writing-a-custom-junit-test-runner/
Followed above both URLs to have below a basic Custom Runner
public class CustomJUnitRunner extends Runner {
private Class testClass;
private HashMap<Method, Description> methodDescriptions;
/**
*
*/
public CustomJUnitRunner(Class testClass) {
this.testClass = testClass;
methodDescriptions = new HashMap<>();
}
@Override
public Description getDescription() {
Description description =
Description.createSuiteDescription(
testClass.getName(),
testClass.getAnnotations());
for(Method method : testClass.getMethods()) {
Annotation annotation =
method.getAnnotation(Test.class);
if(annotation != null) {
Description methodDescription =
Description.createTestDescription(
testClass,
method.getName(),
annotation);
description.addChild(methodDescription);
methodDescriptions.put(method, methodDescription);
}
}
return description;
}
@Override
public void run(RunNotifier notifier) {
System.out.println("running the tests from CustomJUnitRunner: " + testClass);
try {
Object testObject = testClass.getDeclaredConstructor().newInstance();
methodDescriptions.forEach((method, description) -> {
try {
if (method.isAnnotationPresent(Test.class)) {
notifier.fireTestStarted(Description
.createTestDescription(testClass, method.getName()));
method.invoke(testObject);
notifier.fireTestFinished(Description
.createTestDescription(testClass, method.getName()));
}
} catch (Exception e) {
Failure failure = new Failure(description, e.getCause());
notifier.fireTestFailure(failure);
} finally {
notifier.fireTestFinished(description);
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
}
So good to know that one can write such Runners for the custom usage.
Try to google around this & check. -
https://www.baeldung.com/junit-4-custom-runners
https://putridparrot.com/blog/writing-a-custom-junit-test-runner/
Followed above both URLs to have below a basic Custom Runner
public class CustomJUnitRunner extends Runner {
private Class testClass;
private HashMap<Method, Description> methodDescriptions;
/**
*
*/
public CustomJUnitRunner(Class testClass) {
this.testClass = testClass;
methodDescriptions = new HashMap<>();
}
@Override
public Description getDescription() {
Description description =
Description.createSuiteDescription(
testClass.getName(),
testClass.getAnnotations());
for(Method method : testClass.getMethods()) {
Annotation annotation =
method.getAnnotation(Test.class);
if(annotation != null) {
Description methodDescription =
Description.createTestDescription(
testClass,
method.getName(),
annotation);
description.addChild(methodDescription);
methodDescriptions.put(method, methodDescription);
}
}
return description;
}
@Override
public void run(RunNotifier notifier) {
System.out.println("running the tests from CustomJUnitRunner: " + testClass);
try {
Object testObject = testClass.getDeclaredConstructor().newInstance();
methodDescriptions.forEach((method, description) -> {
try {
if (method.isAnnotationPresent(Test.class)) {
notifier.fireTestStarted(Description
.createTestDescription(testClass, method.getName()));
method.invoke(testObject);
notifier.fireTestFinished(Description
.createTestDescription(testClass, method.getName()));
}
} catch (Exception e) {
Failure failure = new Failure(description, e.getCause());
notifier.fireTestFailure(failure);
} finally {
notifier.fireTestFinished(description);
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
}