Reputation: 3241
I want to call the RunNotifier of a JUnitCore. It's called fNotifier and is initiated in the JUnitCore class. Source:
https://github.com/KentBeck/junit/blob/master/src/main/java/org/junit/runner/JUnitCore.java
Can I get this by reflection somehow, or is there any other way I can call this RunNotifier.
I want to use his pleaseStop()
method.
Upvotes: 2
Views: 1173
Reputation: 543
Using JUnit 4.1.2 the field name has changed to 'notifier :
Field field = JUnitCore.class.getDeclaredField("notifier");
Upvotes: 1
Reputation: 61705
Using reflection isn't the best method, but you can do it something like this:
public static void main(String[] args) throws Exception {
Computer computer = new Computer();
JUnitCore jUnitCore = new JUnitCore();
Field field = JUnitCore.class.getDeclaredField("fNotifier");
field.setAccessible(true);
RunNotifier runNotifier = (RunNotifier) field.get(jUnitCore);
runNotifier.pleaseStop();
jUnitCore.run(computer, BeforeAfterTest.class, AssertionErrorTest.class);
}
This will throw a org.junit.runner.notification.StoppedByUserException.
If you want more flexibility or control, a better way to do the above is to just copy the bits you want from JUnitCore into your class, and then you've got direct control over the notifier, and listeners, etc. This is the nicest way to do it. JUnitCore isn't really deisgned to be extended.
Upvotes: 2