Skip to content

Commit 833dead

Browse files
committed
Ensure test fails for the correct reason
Prior to this commit, a dynamic test in FailingBeforeAndAfterMethodsSpringExtensionTestCase was failing but for the wrong reason. Namely, the @configuration class was private which resulted in an IllegalStateException being thrown, when in fact an AssertionFailedError was expected. This commit addresses this by introducing an explicit check for an AssertionFailedError. Issue: SPR-4365
1 parent 7ec85a3 commit 833dead

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTestCase.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.test.context.junit.jupiter;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
1921
import java.util.stream.Stream;
2022

2123
import javax.sql.DataSource;
@@ -24,11 +26,15 @@
2426
import org.junit.jupiter.api.Test;
2527
import org.junit.jupiter.api.TestFactory;
2628
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.junit.platform.engine.TestExecutionResult;
2730
import org.junit.platform.launcher.Launcher;
31+
import org.junit.platform.launcher.TestIdentifier;
2832
import org.junit.platform.launcher.core.LauncherFactory;
2933
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
3034
import org.junit.platform.launcher.listeners.TestExecutionSummary;
3135

36+
import org.opentest4j.AssertionFailedError;
37+
3238
import org.springframework.context.annotation.Bean;
3339
import org.springframework.context.annotation.Configuration;
3440
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@@ -85,7 +91,7 @@ Stream<DynamicTest> generateTests() throws Exception {
8591

8692
private void runTestAndAssertCounters(Class<?> testClass) {
8793
Launcher launcher = LauncherFactory.create();
88-
SummaryGeneratingListener listener = new SummaryGeneratingListener();
94+
ExceptionTrackingListener listener = new ExceptionTrackingListener();
8995
launcher.registerTestExecutionListeners(listener);
9096

9197
launcher.execute(request().selectors(selectClass(testClass)).build());
@@ -106,6 +112,18 @@ private void runTestAndAssertCounters(Class<?> testClass) {
106112
() -> assertEquals(expectedFailedCount, summary.getTestsFailedCount(), () -> name + ": tests failed")
107113
);
108114
// @formatter:on
115+
116+
// Ensure it was an AssertionFailedError that failed the test and not
117+
// something else like an error in the @Configuration class, etc.
118+
if (expectedFailedCount > 0) {
119+
assertEquals(1, listener.exceptions.size(), "exceptions expected");
120+
Throwable exception = listener.exceptions.get(0);
121+
if (!(exception instanceof AssertionFailedError)) {
122+
throw new AssertionFailedError(
123+
exception.getClass().getName() + " is not an instance of " + AssertionFailedError.class.getName(),
124+
exception);
125+
}
126+
}
109127
}
110128

111129
private int getExpectedStartedCount(Class<?> testClass) {
@@ -247,8 +265,9 @@ void afterTransaction() {
247265
}
248266
}
249267

268+
// Must not be private.
250269
@Configuration
251-
private static class DatabaseConfig {
270+
static class DatabaseConfig {
252271

253272
@Bean
254273
PlatformTransactionManager transactionManager() {
@@ -261,4 +280,16 @@ DataSource dataSource() {
261280
}
262281
}
263282

283+
private static class ExceptionTrackingListener extends SummaryGeneratingListener {
284+
285+
List<Throwable> exceptions = new ArrayList<>();
286+
287+
288+
@Override
289+
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
290+
super.executionFinished(testIdentifier, testExecutionResult);
291+
testExecutionResult.getThrowable().ifPresent(exceptions::add);
292+
}
293+
}
294+
264295
}

0 commit comments

Comments
 (0)