Skip to content

Commit 9347324

Browse files
authored
Merge pull request #65 from pitest/bug/failingbeforeall
support failing BeforeAlls
2 parents d7e6d9c + d0e1a5e commit 9347324

5 files changed

Lines changed: 105 additions & 12 deletions

File tree

src/main/java/org/pitest/junit5/JUnit5TestUnit.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ public void executionStarted(TestIdentifier testIdentifier) {
6969

7070
@Override
7171
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
72+
Optional<Throwable> throwable = testExecutionResult.getThrowable();
7273
if (testIdentifier.isTest()) {
73-
Optional<Throwable> throwable = testExecutionResult.getThrowable();
74-
7574
if (TestExecutionResult.Status.ABORTED == testExecutionResult.getStatus()) {
7675
// abort treated as success
7776
// see: https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/Assumptions.html
@@ -81,6 +80,11 @@ public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult
8180
} else {
8281
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass));
8382
}
83+
} else {
84+
// Classes with failing BeforeAll methods identify as containers, not tests.
85+
if (throwable.isPresent()) {
86+
resultCollector.notifyEnd(new Description(testIdentifier.getUniqueId(), testClass), throwable.get());
87+
}
8488
}
8589
}
8690

src/main/java/org/pitest/junit5/JUnit5TestUnitFinder.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
import org.junit.platform.commons.util.PreconditionViolationException;
2626
import org.junit.platform.engine.Filter;
27+
import org.junit.platform.engine.TestExecutionResult;
2728
import org.junit.platform.engine.discovery.DiscoverySelectors;
29+
import org.junit.platform.engine.support.descriptor.ClassSource;
2830
import org.junit.platform.engine.support.descriptor.MethodSource;
2931
import org.junit.platform.launcher.Launcher;
3032
import org.junit.platform.launcher.TagFilter;
@@ -108,6 +110,18 @@ public void executionStarted(TestIdentifier testIdentifier) {
108110
identifiers.add(testIdentifier);
109111
}
110112
}
113+
114+
115+
@Override
116+
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
117+
// Classes with failing BeforeAlls never start execution and identify as 'containers' not 'tests'
118+
if (testExecutionResult.getStatus() == TestExecutionResult.Status.FAILED) {
119+
if (!identifiers.contains(testIdentifier)) {
120+
identifiers.add(testIdentifier);
121+
}
122+
}
123+
}
124+
111125
}
112126

113127
}

src/test/java/org/pitest/junit5/JUnit5TestUnitTest.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import org.junit.jupiter.api.Test;
2525
import org.pitest.junit5.repository.TestClassWithAbortingTest;
26+
import org.pitest.junit5.repository.TestClassWithBeforeAll;
27+
import org.pitest.junit5.repository.TestClassWithFailingBeforeAll;
2628
import org.pitest.junit5.repository.TestClassWithFailingTest;
2729
import org.pitest.junit5.repository.TestClassWithInheritedTestMethod;
2830
import org.pitest.junit5.repository.TestClassWithNestedAnnotationAndNestedTestAnnotation;
@@ -39,10 +41,10 @@
3941
*
4042
* @author tobias
4143
*/
42-
public class JUnit5TestUnitTest {
44+
class JUnit5TestUnitTest {
4345

4446
@Test
45-
public void testTestClassWithTestAnnotation() {
47+
void testTestClassWithTestAnnotation() {
4648
TestResultCollector resultCollector = findTestsIn(TestClassWithTestAnnotation.class);
4749

4850
assertThat(resultCollector.getSkipped()).isEmpty();
@@ -51,7 +53,7 @@ public void testTestClassWithTestAnnotation() {
5153
}
5254

5355
@Test
54-
public void test3TestClassWithTestFactoryAnnotation() {
56+
void test3TestClassWithTestFactoryAnnotation() {
5557
TestResultCollector resultCollector = findTestsIn(TestClassWithTestFactoryAnnotation.class);
5658

5759
assertThat(resultCollector.getSkipped()).isEmpty();
@@ -60,7 +62,7 @@ public void test3TestClassWithTestFactoryAnnotation() {
6062
}
6163

6264
@Test
63-
public void testTestClassWithNestedAnnotationAndNestedTestAnnotation() {
65+
void testTestClassWithNestedAnnotationAndNestedTestAnnotation() {
6466
TestResultCollector resultCollector =
6567
findTestsIn(TestClassWithNestedAnnotationAndNestedTestAnnotation.class);
6668

@@ -71,7 +73,7 @@ public void testTestClassWithNestedAnnotationAndNestedTestAnnotation() {
7173

7274

7375
@Test
74-
public void testTestClassWithNestedAnnotationAndNestedTestFactoryAnnotation() {
76+
void testTestClassWithNestedAnnotationAndNestedTestFactoryAnnotation() {
7577
TestResultCollector resultCollector =
7678
findTestsIn(TestClassWithNestedAnnotationAndNestedTestFactoryAnnotation.class);
7779

@@ -81,7 +83,7 @@ public void testTestClassWithNestedAnnotationAndNestedTestFactoryAnnotation() {
8183
}
8284

8385
@Test
84-
public void testTestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation() {
86+
void testTestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation() {
8587
TestResultCollector resultCollector =
8688
findTestsIn(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation.class);
8789

@@ -91,7 +93,7 @@ public void testTestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAn
9193
}
9294

9395
@Test
94-
public void testTestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation() {
96+
void testTestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation() {
9597
TestResultCollector resultCollector =
9698
findTestsIn(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation.class);
9799

@@ -101,7 +103,7 @@ public void testTestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFa
101103
}
102104

103105
@Test
104-
public void testTestClassWithInheritedTestMethod() {
106+
void testTestClassWithInheritedTestMethod() {
105107
TestResultCollector resultCollector =
106108
findTestsIn(TestClassWithInheritedTestMethod.class);
107109

@@ -111,7 +113,7 @@ public void testTestClassWithInheritedTestMethod() {
111113
}
112114

113115
@Test
114-
public void testTestClassWithFailingTest() {
116+
void testTestClassWithFailingTest() {
115117
TestResultCollector resultCollector = findTestsIn(TestClassWithFailingTest.class);
116118

117119
assertThat(resultCollector.getSkipped()).isEmpty();
@@ -121,14 +123,33 @@ public void testTestClassWithFailingTest() {
121123
}
122124

123125
@Test
124-
public void testTestClassWithAbortingTest() {
126+
void testTestClassWithAbortingTest() {
125127
TestResultCollector resultCollector = findTestsIn(TestClassWithAbortingTest.class);
126128

127129
assertThat(resultCollector.getSkipped()).isEmpty();
128130
assertThat(resultCollector.getStarted()).hasSize(1);
129131
assertThat(resultCollector.getEnded()).hasSize(1);
130132
assertThat(resultCollector.getFailure()).isEmpty();
131133
}
134+
135+
@Test
136+
void testRunsBeforeAlls() {
137+
TestResultCollector resultCollector = findTestsIn(TestClassWithBeforeAll.class);
138+
139+
assertThat(resultCollector.getSkipped()).isEmpty();
140+
assertThat(resultCollector.getStarted()).hasSize(2);
141+
assertThat(resultCollector.getFailure()).isEmpty();
142+
}
143+
144+
@Test
145+
void testFailsWhenBeforeAllFails() {
146+
TestResultCollector resultCollector = findTestsIn(TestClassWithFailingBeforeAll.class);
147+
148+
assertThat(resultCollector.getSkipped()).isEmpty();
149+
assertThat(resultCollector.getStarted()).hasSize(0);
150+
assertThat(resultCollector.getFailure()).isPresent();
151+
}
152+
132153

133154
private TestResultCollector findTestsIn(Class<?> clazz) {
134155
TestResultCollector resultCollector = new TestResultCollector();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.pitest.junit5.repository;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.fail;
7+
8+
public class TestClassWithBeforeAll {
9+
static boolean fail = true;
10+
11+
@BeforeAll
12+
static void fails() {
13+
fail = false;
14+
}
15+
16+
@Test
17+
void aTest() {
18+
if (fail) {
19+
fail();
20+
}
21+
}
22+
23+
@Test
24+
void anotherTest() {
25+
if (fail) {
26+
fail();
27+
}
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.pitest.junit5.repository;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.fail;
7+
8+
public class TestClassWithFailingBeforeAll {
9+
10+
@BeforeAll
11+
static void fails() {
12+
fail();
13+
}
14+
15+
@Test
16+
void aTest() {
17+
18+
}
19+
20+
@Test
21+
void anotherTest() {
22+
23+
}
24+
25+
}

0 commit comments

Comments
 (0)