Skip to content

Commit 20c53a1

Browse files
authored
Merge pull request #12 from Nordstrom/pr/fix-ignore-npe
Fix NullPointerException failures for ignored test methods
2 parents 91725e0 + fa9778c commit 20c53a1

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

src/main/java/com/nordstrom/automation/junit/RetryHandler.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private RetryHandler() {
3535
/**
3636
* Run the specified method, retrying on failure.
3737
*
38-
* @param runner underlying test runner
38+
* @param runner JUnit test runner
3939
* @param method test method to be run
4040
* @param notifier run notifier through which events are published
4141
* @param maxRetry maximum number of retry attempts
@@ -54,19 +54,19 @@ static void runChildWithRetry(Object runner, final FrameworkMethod method, RunNo
5454
statement.evaluate();
5555
doRetry = false;
5656
} catch (AssumptionViolatedException thrown) {
57-
doRetry = doRetry(runner, method, thrown, count);
57+
doRetry = doRetry(method, thrown, count);
5858
if (doRetry) {
5959
description = RetriedTest.proxyFor(description, thrown);
60-
RunReflectiveCall.fireTestIgnored(method);
60+
RunReflectiveCall.fireTestIgnored(runner, method);
6161
eachNotifier.fireTestIgnored();
6262
} else {
6363
eachNotifier.addFailedAssumption(thrown);
6464
}
6565
} catch (Throwable thrown) {
66-
doRetry = doRetry(runner, method, thrown, count);
66+
doRetry = doRetry(method, thrown, count);
6767
if (doRetry) {
6868
description = RetriedTest.proxyFor(description, thrown);
69-
RunReflectiveCall.fireTestIgnored(method);
69+
RunReflectiveCall.fireTestIgnored(runner, method);
7070
eachNotifier.fireTestIgnored();
7171
} else {
7272
eachNotifier.addFailure(thrown);
@@ -85,7 +85,7 @@ static void runChildWithRetry(Object runner, final FrameworkMethod method, RunNo
8585
* @param retryCounter retry counter (remaining attempts)
8686
* @return {@code true} if failed test should be retried; otherwise {@code false}
8787
*/
88-
static boolean doRetry(Object runner, FrameworkMethod method, Throwable thrown, AtomicInteger retryCounter) {
88+
static boolean doRetry(FrameworkMethod method, Throwable thrown, AtomicInteger retryCounter) {
8989
boolean doRetry = false;
9090
if ((retryCounter.decrementAndGet() > -1) && isRetriable(method, thrown)) {
9191
LOGGER.warn("### RETRY ### {}", method);
@@ -100,6 +100,7 @@ static boolean doRetry(Object runner, FrameworkMethod method, Throwable thrown,
100100
* <b>NOTE</b>: If the specified method or the class that declares it are marked with the {@code @NoRetry}
101101
* annotation, this method returns zero (0).
102102
*
103+
* @param runner JUnit test runner
103104
* @param method test method for which retry is being considered
104105
* @return maximum retry attempts that will be made if the specified method fails
105106
*/

src/main/java/com/nordstrom/automation/junit/RunChild.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static void intercept(@This final Object runner, @SuperCall final Callabl
3232
boolean isIgnored = (null != method.getAnnotation(Ignore.class));
3333

3434
if (isIgnored) {
35-
RunReflectiveCall.fireTestIgnored(method);
35+
RunReflectiveCall.fireTestIgnored(runner, method);
3636
}
3737

3838
if (count == 0) {

src/main/java/com/nordstrom/automation/junit/RunReflectiveCall.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.nordstrom.automation.junit;
22

33
import static com.nordstrom.automation.junit.LifecycleHooks.getFieldValue;
4+
import static com.nordstrom.automation.junit.LifecycleHooks.getTestClassOf;
45

56
import java.util.Map;
67
import java.util.Objects;
@@ -149,11 +150,11 @@ private static void notifyIfTestFailed(RunWatcher watcher, AtomicTest atomicTest
149150
/**
150151
* Invoke to tell listeners that an atomic test was ignored.
151152
*
152-
* @param runnable {@link Runnable} object that wraps the atomic test
153+
* @param runner JUnit test runner
154+
* @param method {@link FrameworkMethod} object
153155
*/
154-
static void fireTestIgnored(FrameworkMethod method) {
155-
Object target = getTargetFor(method);
156-
TestClass testClass = LifecycleHooks.getTestClassFor(target);
156+
static void fireTestIgnored(Object runner, FrameworkMethod method) {
157+
TestClass testClass = getTestClassOf(runner);
157158
for (RunWatcher watcher : runWatcherLoader) {
158159
watcher.testIgnored(method, testClass);
159160
}
@@ -203,8 +204,8 @@ static AtomicTest createAtomicTest(TestClass testClass, Runnable runnable) {
203204
AtomicTest atomicTest = null;
204205

205206
try {
206-
runner = LifecycleHooks.getFieldValue(runnable, "this$0");
207-
child = LifecycleHooks.getFieldValue(runnable, "val$each");
207+
runner = getFieldValue(runnable, "this$0");
208+
child = getFieldValue(runnable, "val$each");
208209
} catch (IllegalAccessException | NoSuchFieldException | SecurityException e) {
209210
// nothing to do here
210211
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.nordstrom.automation.junit;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Ignore;
6+
import org.junit.Test;
7+
8+
public class AutomaticRetryIgnore {
9+
10+
@Test
11+
@Ignore
12+
public void testIgnore() {
13+
System.out.println("testIgnore");
14+
assertTrue(true);
15+
}
16+
17+
}

src/test/java/com/nordstrom/automation/junit/AutomaticRetryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ public void testNoRetry() {
7979
assertEquals(rla.getRetriedTests().size(), 0, "Incorrect retried test count");
8080
}
8181

82+
@Test
83+
public void testIgnore() {
84+
RunListenerAdapter rla = new RunListenerAdapter();
85+
86+
JUnitCore runner = new JUnitCore();
87+
runner.addListener(rla);
88+
Result result = runner.run(AutomaticRetryIgnore.class);
89+
assertTrue(result.wasSuccessful());
90+
91+
assertEquals(rla.getPassedTests().size(), 0, "Incorrect passed test count");
92+
assertEquals(rla.getFailedTests().size(), 0, "Incorrect failed test count");
93+
assertEquals(rla.getIgnoredTests().size(), 1, "Incorrect ignored test count");
94+
assertEquals(rla.getRetriedTests().size(), 0, "Incorrect retried test count");
95+
}
96+
8297
@AfterClass
8398
public static void afterClass() {
8499
System.clearProperty(JUnitSettings.MAX_RETRY.key());

0 commit comments

Comments
 (0)