Skip to content

Commit 211659b

Browse files
authored
Merge pull request #50 from giurim/fix-beforeall-exception-handling
fix: Fix beforeAll exception handling and task reporting
2 parents e75c16c + 9e4491b commit 211659b

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package co.helmethair.scalatest.example
2+
3+
import org.scalatest.funspec.AnyFunSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
class CapitalNameTest extends AnyFunSpec with Matchers {
7+
describe("Capital name") {
8+
it("runs") {
9+
true shouldBe true
10+
}
11+
}
12+
}

src/main/java/co/helmethair/scalatest/runtime/Executor.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,29 @@ private void executeSuite(TestDescriptor test, JUnitReporter reporter) {
7474
List<ScalatestTestDescriptor> tests = children.stream().filter(c -> c instanceof ScalatestTestDescriptor)
7575
.map(c -> (ScalatestTestDescriptor) c).collect(Collectors.toList());
7676

77-
Set<TestDescriptor> nonTests = new HashSet<>(children);
78-
nonTests.removeAll(tests);
77+
Set<TestDescriptor> subSuites = new HashSet<>(children);
78+
subSuites.removeAll(tests);
7979

80+
subSuites.stream()
81+
.sorted(Comparator.comparing(TestDescriptor::getDisplayName))
82+
.forEach(c -> executeTest(c, reporter));
83+
84+
boolean suitExecutedOk = true;
8085
if (!tests.isEmpty()) {
81-
runScalatests((ScalatestSuiteDescriptor) test,
86+
suitExecutedOk = runScalatests((ScalatestSuiteDescriptor) test,
8287
tests.stream()
8388
.sorted(Comparator.comparing(TestDescriptor::getDisplayName))
8489
.collect(Collectors.toList()),
8590
reporter);
8691
}
8792

88-
nonTests.stream()
89-
.sorted(Comparator.comparing(TestDescriptor::getDisplayName))
90-
.forEach(c -> executeTest(c, reporter));
91-
92-
reporter.getJunitListener().executionFinished(test, TestExecutionResult.successful());
93+
if (suitExecutedOk){
94+
// if exception is thrown during suit execution (init, before/after all) we should not report a SUCCESS
95+
reporter.getJunitListener().executionFinished(test, TestExecutionResult.successful());
96+
}
9397
}
9498

95-
private void runScalatests(ScalatestSuiteDescriptor containingSuite, List<ScalatestTestDescriptor> tests, JUnitReporter reporter) {
99+
private boolean runScalatests(ScalatestSuiteDescriptor containingSuite, List<ScalatestTestDescriptor> tests, JUnitReporter reporter) {
96100

97101
Suite scalasuite = containingSuite.getScalasuite();
98102

@@ -125,6 +129,7 @@ private void runScalatests(ScalatestSuiteDescriptor containingSuite, List<Scalat
125129
try {
126130
Status status = scalasuite.run(Option.apply(null), args);
127131
status.waitUntilCompleted();
132+
return true;
128133
} catch (Throwable e) {
129134
if (e instanceof InstantiationException || e instanceof IllegalAccessException) {
130135
reporter.apply(suiteAborted(args.tracker().nextOrdinal(), e, Resources.cannotInstantiateSuite(e.getMessage()), scalasuite));
@@ -139,6 +144,7 @@ private void runScalatests(ScalatestSuiteDescriptor containingSuite, List<Scalat
139144
throw e;
140145
}
141146
}
147+
return false;
142148
}
143149
}
144150

src/test/java/co/helmethair/scalatest/BeforeAfterTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.platform.engine.EngineDiscoveryRequest;
77
import org.junit.platform.engine.ExecutionRequest;
88
import org.junit.platform.engine.TestDescriptor;
9+
import org.junit.platform.engine.TestExecutionResult;
910

1011
import java.util.HashMap;
1112
import java.util.Map;
@@ -61,6 +62,7 @@ void beforeFailedTest() {
6162
verifyTestExecuteCode(calls, () -> engine.execute(executionRequest));
6263

6364
verifyTestFailReportedWith("[engine:scalatest]/[suite:tests.FailInBeforeTest]", listener, null);
65+
verifyTestSuccessNotReported("[engine:scalatest]/[suite:tests.FailInBeforeTest]", listener);
6466
}
6567

6668
@Test
@@ -78,6 +80,7 @@ void beforeAllFailedTest() {
7880

7981
verifyTestExecuteCode(calls, () -> engine.execute(executionRequest));
8082
verifyTestFailReportedWith("[engine:scalatest]/[suite:tests.FailInBeforeAllTest]", listener, null);
83+
verifyTestSuccessNotReported("[engine:scalatest]/[suite:tests.FailInBeforeAllTest]", listener);
8184
}
8285

8386
@Test
@@ -96,6 +99,7 @@ void afterFailedTest() {
9699

97100
verifyTestSuccessReported("[engine:scalatest]/[suite:tests.FailInAfterTest]/[test:test]", listener);
98101
verifyTestFailReportedWith("[engine:scalatest]/[suite:tests.FailInAfterTest]", listener, null);
102+
verifyTestSuccessNotReported("[engine:scalatest]/[suite:tests.FailInAfterTest]", listener);
99103
}
100104

101105
@Test
@@ -116,5 +120,6 @@ void afterAllFailedTest() {
116120
verifyTestSuccessReported("[engine:scalatest]/[suite:tests.FailInAfterAllTest]/[test:test 1]", listener);
117121
verifyTestSuccessReported("[engine:scalatest]/[suite:tests.FailInAfterAllTest]/[test:test 2]", listener);
118122
verifyTestFailReportedWith("[engine:scalatest]/[suite:tests.FailInAfterAllTest]", listener, null);
123+
verifyTestSuccessNotReported("[engine:scalatest]/[suite:tests.FailInAfterAllTest]", listener);
119124
}
120125
}

src/test/java/co/helmethair/scalatest/helper/TestHelpers.java

+9
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ default void verifyTestSuccessReported(String testIdsuffix, TestEngineExecutionL
186186
);
187187
}
188188

189+
default void verifyTestSuccessNotReported(String testIdsuffix, TestEngineExecutionListener listener) {
190+
verify(listener, never()).executionFinished(
191+
argThat(a -> a.getUniqueId().toString().endsWith(testIdsuffix)),
192+
argThat(a -> !a.getThrowable().isPresent()
193+
&& a.getStatus() == TestExecutionResult.Status.SUCCESSFUL
194+
)
195+
);
196+
}
197+
189198
default void verifyTestSuccessReported(String testIdsuffix, TestExecutionListener listener) {
190199
verify(listener, atLeastOnce()).executionFinished(
191200
argThat(a -> a.getUniqueId().endsWith(testIdsuffix)),

0 commit comments

Comments
 (0)