Skip to content

Commit b2551b4

Browse files
authored
Merge pull request #43 from Nordstrom/pr/issue-42
Resolve #42: Change tracking of "atomic" test to handle rule failures
2 parents 0d01297 + d998c45 commit b2551b4

File tree

6 files changed

+67
-24
lines changed

6 files changed

+67
-24
lines changed

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,11 @@ public class AtomicTest {
2828
private final List<FrameworkMethod> particles;
2929
private Throwable thrown;
3030

31-
public AtomicTest(Object runner, Description description) {
31+
public AtomicTest(Object runner, FrameworkMethod identity) {
3232
this.runner = runner;
33-
this.description = description;
34-
this.particles = invoke(runner, "getChildren");
35-
36-
FrameworkMethod identity = null;
37-
String methodName = description.getMethodName().split("[\\[\\(]")[0];
38-
for (FrameworkMethod method : particles) {
39-
if (methodName.equals(method.getName())) {
40-
identity = method;
41-
break;
42-
}
43-
}
44-
4533
this.identity = identity;
34+
this.description = invoke(runner, "describeChild", identity);
35+
this.particles = invoke(runner, "getChildren");
4636
}
4737

4838
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static void pushThreadRunner(final Object runner) {
102102
}
103103

104104
/**
105-
* Pop the top JUnit test runner onto the stack for the current thread.
105+
* Pop the top JUnit test runner from the stack for the current thread.
106106
*
107107
* @return {@code ParentRunner} object
108108
* @throws EmptyStackException if called outside the scope of an active runner

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.runner.Description;
1010
import org.junit.runner.notification.Failure;
1111
import org.junit.runner.notification.RunListener;
12+
import org.junit.runners.model.FrameworkMethod;
1213

1314
public class RunAnnouncer extends RunListener {
1415

@@ -24,7 +25,7 @@ public class RunAnnouncer extends RunListener {
2425
*/
2526
@Override
2627
public void testStarted(Description description) throws Exception {
27-
AtomicTest atomicTest = newAtomicTest(description);
28+
AtomicTest atomicTest = getAtomicTestOf(description);
2829
synchronized(runWatcherLoader) {
2930
for (RunWatcher watcher : runWatcherLoader) {
3031
watcher.testStarted(atomicTest);
@@ -37,7 +38,7 @@ public void testStarted(Description description) throws Exception {
3738
*/
3839
@Override
3940
public void testFinished(Description description) throws Exception {
40-
AtomicTest atomicTest = getAtomicTestOf(Run.getThreadRunner());
41+
AtomicTest atomicTest = getAtomicTestOf(description);
4142
synchronized(runWatcherLoader) {
4243
for (RunWatcher watcher : runWatcherLoader) {
4344
watcher.testFinished(atomicTest);
@@ -76,7 +77,7 @@ public void testAssumptionFailure(Failure failure) {
7677
*/
7778
@Override
7879
public void testIgnored(Description description) throws Exception {
79-
AtomicTest atomicTest = newAtomicTest(description);
80+
AtomicTest atomicTest = getAtomicTestOf(description);
8081
synchronized(runWatcherLoader) {
8182
for (RunWatcher watcher : runWatcherLoader) {
8283
watcher.testIgnored(atomicTest);
@@ -90,21 +91,21 @@ public void testIgnored(Description description) throws Exception {
9091
* @param description {@link Description} object
9192
* @return {@link AtomicTest} object
9293
*/
93-
private static AtomicTest newAtomicTest(Description description) {
94-
Object runner = Run.getThreadRunner();
95-
AtomicTest atomicTest = new AtomicTest(runner, description);
94+
static AtomicTest newAtomicTest(Object runner, FrameworkMethod method) {
95+
AtomicTest atomicTest = new AtomicTest(runner, method);
9696
RUNNER_TO_ATOMICTEST.put(runner, atomicTest);
97+
RUNNER_TO_ATOMICTEST.put(atomicTest.getDescription(), atomicTest);
9798
return atomicTest;
9899
}
99100

100101
/**
101-
* Get the atomic test object for the specified class runner.
102+
* Get the atomic test object for the specified class runner or method description.
102103
*
103-
* @param runner JUnit class runner
104+
* @param testKey JUnit class runner or method description
104105
* @return {@link AtomicTest} object (may be {@code null})
105106
*/
106-
static AtomicTest getAtomicTestOf(Object runner) {
107-
return RUNNER_TO_ATOMICTEST.get(runner);
107+
static AtomicTest getAtomicTestOf(Object testKey) {
108+
return RUNNER_TO_ATOMICTEST.get(testKey);
108109
}
109110

110111
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public static void intercept(@This final Object runner, @SuperCall final Callabl
5050
int count = RetryHandler.getMaxRetry(runner, method);
5151
boolean isIgnored = (null != method.getAnnotation(Ignore.class));
5252

53+
RunAnnouncer.newAtomicTest(runner, method);
54+
5355
if (count == 0) {
5456
LifecycleHooks.callProxy(proxy);
5557
} else if (!isIgnored) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.nordstrom.automation.junit;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
6+
import org.junit.runner.JUnitCore;
7+
import org.junit.runner.Result;
8+
import org.junit.runner.notification.Failure;
9+
import org.testng.annotations.Test;
10+
11+
public class RuleFailureTest {
12+
13+
@Test
14+
public void verifyRuleExceptionHandling() {
15+
JUnitCore runner = new JUnitCore();
16+
Result result = runner.run(RuleThrowsException.class);
17+
assertFalse(result.wasSuccessful());
18+
assertEquals(1, result.getFailureCount());
19+
Failure failure = result.getFailures().get(0);
20+
assertEquals(RuntimeException.class, failure.getException().getClass());
21+
assertEquals("Must be failed", failure.getMessage());
22+
}
23+
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.nordstrom.automation.junit;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.TestRule;
8+
import org.junit.runner.Description;
9+
import org.junit.runners.model.Statement;
10+
11+
public class RuleThrowsException extends TestBase {
12+
13+
@Rule
14+
public final TestRule failing = new TestRule() {
15+
@Override
16+
public Statement apply(Statement base, Description description) {
17+
throw new RuntimeException("Must be failed");
18+
}
19+
};
20+
21+
@Test
22+
public void happy() {
23+
assertTrue(true);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)