Skip to content

Commit 80018c6

Browse files
committed
Introduce before/after test execution support in the SpringExtension
Issue: SPR-4365
1 parent da89332 commit 80018c6

File tree

2 files changed

+289
-1
lines changed

2 files changed

+289
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
import org.junit.jupiter.api.extension.AfterAllCallback;
2525
import org.junit.jupiter.api.extension.AfterEachCallback;
26+
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
2627
import org.junit.jupiter.api.extension.BeforeAllCallback;
2728
import org.junit.jupiter.api.extension.BeforeEachCallback;
29+
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
2830
import org.junit.jupiter.api.extension.ContainerExtensionContext;
2931
import org.junit.jupiter.api.extension.ExtensionContext;
3032
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
@@ -54,7 +56,8 @@
5456
* @see org.springframework.test.context.TestContextManager
5557
*/
5658
public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor,
57-
BeforeEachCallback, AfterEachCallback, ParameterResolver {
59+
BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback,
60+
ParameterResolver {
5861

5962
/**
6063
* {@link Namespace} in which {@code TestContextManagers} are stored, keyed
@@ -101,6 +104,27 @@ public void beforeEach(TestExtensionContext context) throws Exception {
101104
getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
102105
}
103106

107+
/**
108+
* Delegates to {@link TestContextManager#beforeTestExecution}.
109+
*/
110+
@Override
111+
public void beforeTestExecution(TestExtensionContext context) throws Exception {
112+
Object testInstance = context.getTestInstance();
113+
Method testMethod = context.getTestMethod().get();
114+
getTestContextManager(context).beforeTestExecution(testInstance, testMethod);
115+
}
116+
117+
/**
118+
* Delegates to {@link TestContextManager#afterTestExecution}.
119+
*/
120+
@Override
121+
public void afterTestExecution(TestExtensionContext context) throws Exception {
122+
Object testInstance = context.getTestInstance();
123+
Method testMethod = context.getTestMethod().get();
124+
Throwable testException = context.getTestException().orElse(null);
125+
getTestContextManager(context).afterTestExecution(testInstance, testMethod, testException);
126+
}
127+
104128
/**
105129
* Delegates to {@link TestContextManager#afterTestMethod}.
106130
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit.jupiter;
18+
19+
import java.util.stream.Stream;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.junit.jupiter.api.DynamicTest;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.TestFactory;
26+
import org.junit.jupiter.api.extension.ExtendWith;
27+
import org.junit.platform.launcher.Launcher;
28+
import org.junit.platform.launcher.core.LauncherFactory;
29+
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
30+
import org.junit.platform.launcher.listeners.TestExecutionSummary;
31+
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
35+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
36+
import org.springframework.test.context.TestContext;
37+
import org.springframework.test.context.TestExecutionListener;
38+
import org.springframework.test.context.TestExecutionListeners;
39+
import org.springframework.test.context.transaction.AfterTransaction;
40+
import org.springframework.test.context.transaction.BeforeTransaction;
41+
import org.springframework.transaction.PlatformTransactionManager;
42+
import org.springframework.transaction.annotation.Transactional;
43+
44+
import static org.junit.jupiter.api.Assertions.*;
45+
import static org.junit.jupiter.api.DynamicTest.*;
46+
import static org.junit.platform.engine.discovery.DiscoverySelectors.*;
47+
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.*;
48+
49+
/**
50+
* Integration tests which verify that '<i>before</i>' and '<i>after</i>'
51+
* methods of {@link TestExecutionListener TestExecutionListeners} as well as
52+
* {@code @BeforeTransaction} and {@code @AfterTransaction} methods can fail
53+
* tests run via the {@link SpringExtension} in a JUnit 5 (Jupiter) environment.
54+
*
55+
* <p>See: <a href="https://jira.spring.io/browse/SPR-3960" target="_blank">SPR-3960</a>
56+
* and <a href="https://jira.spring.io/browse/SPR-4365" target="_blank">SPR-4365</a>.
57+
*
58+
* <p>Indirectly, this class also verifies that all {@code TestExecutionListener}
59+
* lifecycle callbacks are called.
60+
*
61+
* @author Sam Brannen
62+
* @since 5.0
63+
*/
64+
class FailingBeforeAndAfterMethodsSpringExtensionTestCase {
65+
66+
private static Stream<Class<?>> testClasses() {
67+
// @formatter:off
68+
return Stream.of(
69+
AlwaysFailingBeforeTestClassTestCase.class,
70+
AlwaysFailingAfterTestClassTestCase.class,
71+
AlwaysFailingPrepareTestInstanceTestCase.class,
72+
AlwaysFailingBeforeTestMethodTestCase.class,
73+
AlwaysFailingBeforeTestExecutionTestCase.class,
74+
AlwaysFailingAfterTestExecutionTestCase.class,
75+
AlwaysFailingAfterTestMethodTestCase.class,
76+
FailingBeforeTransactionTestCase.class,
77+
FailingAfterTransactionTestCase.class);
78+
// @formatter:on
79+
}
80+
81+
@TestFactory
82+
Stream<DynamicTest> generateTests() throws Exception {
83+
return testClasses().map(clazz -> dynamicTest(clazz.getSimpleName(), () -> runTestAndAssertCounters(clazz)));
84+
}
85+
86+
private void runTestAndAssertCounters(Class<?> testClass) {
87+
Launcher launcher = LauncherFactory.create();
88+
SummaryGeneratingListener listener = new SummaryGeneratingListener();
89+
launcher.registerTestExecutionListeners(listener);
90+
91+
launcher.execute(request().selectors(selectClass(testClass)).build());
92+
TestExecutionSummary summary = listener.getSummary();
93+
94+
String name = testClass.getSimpleName();
95+
int expectedStartedCount = getExpectedStartedCount(testClass);
96+
int expectedSucceededCount = getExpectedSucceededCount(testClass);
97+
int expectedFailedCount = getExpectedFailedCount(testClass);
98+
99+
// @formatter:off
100+
assertAll(
101+
() -> assertEquals(1, summary.getTestsFoundCount(), () -> name + ": tests found"),
102+
() -> assertEquals(0, summary.getTestsSkippedCount(), () -> name + ": tests skipped"),
103+
() -> assertEquals(0, summary.getTestsAbortedCount(), () -> name + ": tests aborted"),
104+
() -> assertEquals(expectedStartedCount, summary.getTestsStartedCount(), () -> name + ": tests started"),
105+
() -> assertEquals(expectedSucceededCount, summary.getTestsSucceededCount(), () -> name + ": tests succeeded"),
106+
() -> assertEquals(expectedFailedCount, summary.getTestsFailedCount(), () -> name + ": tests failed")
107+
);
108+
// @formatter:on
109+
}
110+
111+
private int getExpectedStartedCount(Class<?> testClass) {
112+
return (testClass == AlwaysFailingBeforeTestClassTestCase.class ? 0 : 1);
113+
}
114+
115+
private int getExpectedSucceededCount(Class<?> testClass) {
116+
return (testClass == AlwaysFailingAfterTestClassTestCase.class ? 1 : 0);
117+
}
118+
119+
private int getExpectedFailedCount(Class<?> testClass) {
120+
if (testClass == AlwaysFailingBeforeTestClassTestCase.class
121+
|| testClass == AlwaysFailingAfterTestClassTestCase.class) {
122+
return 0;
123+
}
124+
return 1;
125+
}
126+
127+
128+
// -------------------------------------------------------------------
129+
130+
private static class AlwaysFailingBeforeTestClassTestExecutionListener implements TestExecutionListener {
131+
132+
@Override
133+
public void beforeTestClass(TestContext testContext) {
134+
fail("always failing beforeTestClass()");
135+
}
136+
}
137+
138+
private static class AlwaysFailingAfterTestClassTestExecutionListener implements TestExecutionListener {
139+
140+
@Override
141+
public void afterTestClass(TestContext testContext) {
142+
fail("always failing afterTestClass()");
143+
}
144+
}
145+
146+
private static class AlwaysFailingPrepareTestInstanceTestExecutionListener implements TestExecutionListener {
147+
148+
@Override
149+
public void prepareTestInstance(TestContext testContext) throws Exception {
150+
fail("always failing prepareTestInstance()");
151+
}
152+
}
153+
154+
private static class AlwaysFailingBeforeTestMethodTestExecutionListener implements TestExecutionListener {
155+
156+
@Override
157+
public void beforeTestMethod(TestContext testContext) {
158+
fail("always failing beforeTestMethod()");
159+
}
160+
}
161+
162+
private static class AlwaysFailingBeforeTestExecutionTestExecutionListener implements TestExecutionListener {
163+
164+
@Override
165+
public void beforeTestExecution(TestContext testContext) {
166+
fail("always failing beforeTestExecution()");
167+
}
168+
}
169+
170+
private static class AlwaysFailingAfterTestMethodTestExecutionListener implements TestExecutionListener {
171+
172+
@Override
173+
public void afterTestMethod(TestContext testContext) {
174+
fail("always failing afterTestMethod()");
175+
}
176+
}
177+
178+
private static class AlwaysFailingAfterTestExecutionTestExecutionListener implements TestExecutionListener {
179+
180+
@Override
181+
public void afterTestExecution(TestContext testContext) {
182+
fail("always failing afterTestExecution()");
183+
}
184+
}
185+
186+
@ExtendWith(SpringExtension.class)
187+
private static abstract class BaseTestCase {
188+
189+
@Test
190+
void testNothing() {
191+
}
192+
}
193+
194+
@TestExecutionListeners(AlwaysFailingBeforeTestClassTestExecutionListener.class)
195+
private static class AlwaysFailingBeforeTestClassTestCase extends BaseTestCase {
196+
}
197+
198+
@TestExecutionListeners(AlwaysFailingAfterTestClassTestExecutionListener.class)
199+
private static class AlwaysFailingAfterTestClassTestCase extends BaseTestCase {
200+
}
201+
202+
@TestExecutionListeners(AlwaysFailingPrepareTestInstanceTestExecutionListener.class)
203+
private static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCase {
204+
}
205+
206+
@TestExecutionListeners(AlwaysFailingBeforeTestMethodTestExecutionListener.class)
207+
private static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
208+
}
209+
210+
@TestExecutionListeners(AlwaysFailingBeforeTestExecutionTestExecutionListener.class)
211+
private static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
212+
}
213+
214+
@TestExecutionListeners(AlwaysFailingAfterTestExecutionTestExecutionListener.class)
215+
private static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
216+
}
217+
218+
@TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class)
219+
private static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
220+
}
221+
222+
@SpringJUnitConfig(DatabaseConfig.class)
223+
@Transactional
224+
private static class FailingBeforeTransactionTestCase {
225+
226+
@Test
227+
void testNothing() {
228+
}
229+
230+
@BeforeTransaction
231+
void beforeTransaction() {
232+
fail("always failing beforeTransaction()");
233+
}
234+
}
235+
236+
@SpringJUnitConfig(DatabaseConfig.class)
237+
@Transactional
238+
private static class FailingAfterTransactionTestCase {
239+
240+
@Test
241+
void testNothing() {
242+
}
243+
244+
@AfterTransaction
245+
void afterTransaction() {
246+
fail("always failing afterTransaction()");
247+
}
248+
}
249+
250+
@Configuration
251+
private static class DatabaseConfig {
252+
253+
@Bean
254+
PlatformTransactionManager transactionManager() {
255+
return new DataSourceTransactionManager(dataSource());
256+
}
257+
258+
@Bean
259+
DataSource dataSource() {
260+
return new EmbeddedDatabaseBuilder().generateUniqueName(true).build();
261+
}
262+
}
263+
264+
}

0 commit comments

Comments
 (0)