Skip to content

Commit 6566e32

Browse files
authored
Merge pull request #54 from Nordstrom/pr/fix-before-class-npe
Resolve #53: If thread runner lacks atomic test, use failure description
2 parents 7fb003c + d7f1bb5 commit 6566e32

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ static <T> AtomicTest<T> getAtomicTestOf(Object testKey) {
145145
*/
146146
private static <T> AtomicTest<T> setTestFailure(Failure failure) {
147147
AtomicTest<T> atomicTest = getAtomicTestOf(Run.getThreadRunner());
148+
if (atomicTest == null) {
149+
atomicTest = getAtomicTestOf(failure.getDescription());
150+
}
148151
atomicTest.setThrowable(failure.getException());
149152
return atomicTest;
150153
}
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 BeforeClassFailureTest {
12+
13+
@Test
14+
public void verifyBeforeClassExceptionHandling() {
15+
JUnitCore runner = new JUnitCore();
16+
Result result = runner.run(BeforeClassThrowsException.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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.nordstrom.automation.junit;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class BeforeClassThrowsException extends TestBase {
9+
10+
@BeforeClass
11+
public static void failing() {
12+
throw new RuntimeException("Must be failed");
13+
}
14+
15+
@Test
16+
public void happy() {
17+
assertTrue(true);
18+
}
19+
20+
@Test
21+
public void sad() {
22+
assertTrue(false);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)