From 833deadddc0de6ea791f58e561d39e920b3e013d Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 13 Jul 2016 21:10:47 +0200 Subject: [PATCH] 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 --- ...ndAfterMethodsSpringExtensionTestCase.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTestCase.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTestCase.java index cd5ae0aa24..cd9d2a76ca 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTestCase.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTestCase.java @@ -16,6 +16,8 @@ package org.springframework.test.context.junit.jupiter; +import java.util.ArrayList; +import java.util.List; import java.util.stream.Stream; import javax.sql.DataSource; @@ -24,11 +26,15 @@ import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.platform.engine.TestExecutionResult; import org.junit.platform.launcher.Launcher; +import org.junit.platform.launcher.TestIdentifier; import org.junit.platform.launcher.core.LauncherFactory; import org.junit.platform.launcher.listeners.SummaryGeneratingListener; import org.junit.platform.launcher.listeners.TestExecutionSummary; +import org.opentest4j.AssertionFailedError; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; @@ -85,7 +91,7 @@ class FailingBeforeAndAfterMethodsSpringExtensionTestCase { private void runTestAndAssertCounters(Class testClass) { Launcher launcher = LauncherFactory.create(); - SummaryGeneratingListener listener = new SummaryGeneratingListener(); + ExceptionTrackingListener listener = new ExceptionTrackingListener(); launcher.registerTestExecutionListeners(listener); launcher.execute(request().selectors(selectClass(testClass)).build()); @@ -106,6 +112,18 @@ class FailingBeforeAndAfterMethodsSpringExtensionTestCase { () -> assertEquals(expectedFailedCount, summary.getTestsFailedCount(), () -> name + ": tests failed") ); // @formatter:on + + // Ensure it was an AssertionFailedError that failed the test and not + // something else like an error in the @Configuration class, etc. + if (expectedFailedCount > 0) { + assertEquals(1, listener.exceptions.size(), "exceptions expected"); + Throwable exception = listener.exceptions.get(0); + if (!(exception instanceof AssertionFailedError)) { + throw new AssertionFailedError( + exception.getClass().getName() + " is not an instance of " + AssertionFailedError.class.getName(), + exception); + } + } } private int getExpectedStartedCount(Class testClass) { @@ -247,8 +265,9 @@ class FailingBeforeAndAfterMethodsSpringExtensionTestCase { } } + // Must not be private. @Configuration - private static class DatabaseConfig { + static class DatabaseConfig { @Bean PlatformTransactionManager transactionManager() { @@ -261,4 +280,16 @@ class FailingBeforeAndAfterMethodsSpringExtensionTestCase { } } + private static class ExceptionTrackingListener extends SummaryGeneratingListener { + + List exceptions = new ArrayList<>(); + + + @Override + public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { + super.executionFinished(testIdentifier, testExecutionResult); + testExecutionResult.getThrowable().ifPresent(exceptions::add); + } + } + }