Skip to content

Commit aec267d

Browse files
committed
Migrate spring-webflux test suite from JUnit 4 to JUnit Jupiter
See spring-projectsgh-23451
1 parent f2b6b52 commit aec267d

File tree

154 files changed

+1264
-839
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+1264
-839
lines changed

spring-test/src/test/java/org/springframework/test/context/jdbc/PopulatedSchemaDatabaseConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public PlatformTransactionManager transactionManager() {
4343
@Bean
4444
public DataSource dataSource() {
4545
return new EmbeddedDatabaseBuilder()//
46-
.setName("populated-sql-scripts-test-db")//
46+
.generateUniqueName(true)
4747
.addScript("classpath:/org/springframework/test/context/jdbc/schema.sql") //
4848
.build();
4949
}

spring-web/src/test/java/org/springframework/http/server/reactive/AbstractHttpHandlerIntegrationTests.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.apache.commons.logging.Log;
2828
import org.apache.commons.logging.LogFactory;
2929
import org.junit.jupiter.api.AfterEach;
30+
import org.junit.jupiter.api.extension.RegisterExtension;
31+
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
3032
import org.junit.jupiter.params.ParameterizedTest;
3133
import org.junit.jupiter.params.provider.MethodSource;
3234
import reactor.core.publisher.Flux;
@@ -36,9 +38,40 @@
3638
import org.springframework.http.server.reactive.bootstrap.ReactorHttpServer;
3739
import org.springframework.http.server.reactive.bootstrap.TomcatHttpServer;
3840
import org.springframework.http.server.reactive.bootstrap.UndertowHttpServer;
41+
import org.springframework.util.StringUtils;
42+
import org.springframework.web.client.HttpServerErrorException;
3943

4044
public abstract class AbstractHttpHandlerIntegrationTests {
4145

46+
/**
47+
* Custom JUnit Jupiter extension that handles exceptions thrown by test methods.
48+
*
49+
* <p>If the test method throws an {@link HttpServerErrorException}, this
50+
* extension will throw an {@link AssertionError} that wraps the
51+
* {@code HttpServerErrorException} using the
52+
* {@link HttpServerErrorException#getResponseBodyAsString() response body}
53+
* as the failure message.
54+
*
55+
* <p>This mechanism provides an actually meaningful failure message if the
56+
* test fails due to an {@code AssertionError} on the server.
57+
*/
58+
@RegisterExtension
59+
TestExecutionExceptionHandler serverErrorToAssertionErrorConverter = (context, throwable) -> {
60+
if (throwable instanceof HttpServerErrorException) {
61+
HttpServerErrorException ex = (HttpServerErrorException) throwable;
62+
String responseBody = ex.getResponseBodyAsString();
63+
if (StringUtils.hasText(responseBody)) {
64+
String prefix = AssertionError.class.getName() + ": ";
65+
if (responseBody.startsWith(prefix)) {
66+
responseBody = responseBody.substring(prefix.length());
67+
}
68+
throw new AssertionError(responseBody, ex);
69+
}
70+
}
71+
// Else throw as-is in order to comply with the contract of TestExecutionExceptionHandler.
72+
throw throwable;
73+
};
74+
4275
protected final Log logger = LogFactory.getLog(getClass());
4376

4477
protected HttpServer server;
@@ -85,9 +118,10 @@ public static Flux<Long> testInterval(Duration period, int count) {
85118

86119
@Retention(RetentionPolicy.RUNTIME)
87120
@Target(ElementType.METHOD)
88-
@ParameterizedTest
121+
@ParameterizedTest(name = "{0}")
89122
@MethodSource("org.springframework.http.server.reactive.AbstractHttpHandlerIntegrationTests#httpServers()")
90-
protected @interface ParameterizedHttpServerTest {
123+
// public for Kotlin
124+
public @interface ParameterizedHttpServerTest {
91125
}
92126

93127
static Stream<HttpServer> httpServers() {

spring-webflux/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import java.util.List;
2222
import java.util.concurrent.atomic.AtomicReference;
2323

24-
import org.junit.Before;
25-
import org.junit.Test;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
2626
import org.reactivestreams.Publisher;
2727
import reactor.core.publisher.Mono;
2828
import reactor.test.StepVerifier;
@@ -67,7 +67,7 @@ public class DispatcherHandlerErrorTests {
6767
private DispatcherHandler dispatcherHandler;
6868

6969

70-
@Before
70+
@BeforeEach
7171
public void setup() {
7272
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
7373
ctx.register(TestConfig.class);

spring-webflux/src/test/java/org/springframework/web/reactive/DispatcherHandlerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.time.Duration;
2121
import java.util.function.Supplier;
2222

23-
import org.junit.Test;
23+
import org.junit.jupiter.api.Test;
2424
import reactor.core.publisher.Mono;
2525

2626
import org.springframework.context.support.StaticApplicationContext;

spring-webflux/src/test/java/org/springframework/web/reactive/FlushingIntegrationTests.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.nio.charset.StandardCharsets;
2020
import java.time.Duration;
2121

22-
import org.junit.Before;
23-
import org.junit.Test;
2422
import reactor.core.publisher.Flux;
2523
import reactor.core.publisher.Mono;
2624
import reactor.test.StepVerifier;
@@ -30,6 +28,7 @@
3028
import org.springframework.http.server.reactive.HttpHandler;
3129
import org.springframework.http.server.reactive.ServerHttpRequest;
3230
import org.springframework.http.server.reactive.ServerHttpResponse;
31+
import org.springframework.http.server.reactive.bootstrap.HttpServer;
3332
import org.springframework.web.reactive.function.client.WebClient;
3433

3534
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,22 +38,25 @@
3938
*
4039
* @author Sebastien Deleuze
4140
* @author Rossen Stoyanchev
41+
* @author Sam Brannen
4242
* @since 5.0
4343
*/
44-
public class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTests {
44+
class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTests {
4545

4646
private WebClient webClient;
4747

4848

49-
@Before
50-
public void setup() throws Exception {
51-
super.setup();
49+
@Override
50+
protected void startServer(HttpServer httpServer) throws Exception {
51+
super.startServer(httpServer);
5252
this.webClient = WebClient.create("http://localhost:" + this.port);
5353
}
5454

5555

56-
@Test
57-
public void writeAndFlushWith() {
56+
@ParameterizedHttpServerTest
57+
void writeAndFlushWith(HttpServer httpServer) throws Exception {
58+
startServer(httpServer);
59+
5860
Mono<String> result = this.webClient.get()
5961
.uri("/write-and-flush")
6062
.retrieve()
@@ -68,8 +70,10 @@ public void writeAndFlushWith() {
6870
.verify(Duration.ofSeconds(10L));
6971
}
7072

71-
@Test // SPR-14991
72-
public void writeAndAutoFlushOnComplete() {
73+
@ParameterizedHttpServerTest // SPR-14991
74+
void writeAndAutoFlushOnComplete(HttpServer httpServer) throws Exception {
75+
startServer(httpServer);
76+
7377
Mono<String> result = this.webClient.get()
7478
.uri("/write-and-complete")
7579
.retrieve()
@@ -93,8 +97,10 @@ public void writeAndAutoFlushOnComplete() {
9397
}
9498
}
9599

96-
@Test // SPR-14992
97-
public void writeAndAutoFlushBeforeComplete() {
100+
@ParameterizedHttpServerTest // SPR-14992
101+
void writeAndAutoFlushBeforeComplete(HttpServer httpServer) throws Exception {
102+
startServer(httpServer);
103+
98104
Mono<String> result = this.webClient.get()
99105
.uri("/write-and-never-complete")
100106
.retrieve()

spring-webflux/src/test/java/org/springframework/web/reactive/accept/HeaderContentTypeResolverTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import java.util.List;
2020

21-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.http.MediaType;
2424
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;

spring-webflux/src/test/java/org/springframework/web/reactive/accept/ParameterContentTypeResolverTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.List;
2020
import java.util.Map;
2121

22-
import org.junit.Test;
22+
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.http.MediaType;
2525
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;

spring-webflux/src/test/java/org/springframework/web/reactive/accept/RequestedContentTypeResolverBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.List;
2020

21-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.http.MediaType;
2424
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;

spring-webflux/src/test/java/org/springframework/web/reactive/config/CorsRegistryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Arrays;
2020
import java.util.Map;
2121

22-
import org.junit.Test;
22+
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.web.cors.CorsConfiguration;
2525

spring-webflux/src/test/java/org/springframework/web/reactive/config/DelegatingWebFluxConfigurationTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121

22-
import org.junit.Before;
23-
import org.junit.Test;
24-
import org.junit.runner.RunWith;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
2524
import org.mockito.ArgumentCaptor;
2625
import org.mockito.Captor;
2726
import org.mockito.Mock;
28-
import org.mockito.junit.MockitoJUnitRunner;
27+
import org.mockito.junit.jupiter.MockitoSettings;
28+
import org.mockito.quality.Strictness;
2929

3030
import org.springframework.context.support.StaticApplicationContext;
3131
import org.springframework.core.ReactiveAdapterRegistry;
@@ -49,7 +49,7 @@
4949
*
5050
* @author Brian Clozel
5151
*/
52-
@RunWith(MockitoJUnitRunner.class)
52+
@MockitoSettings(strictness = Strictness.LENIENT)
5353
public class DelegatingWebFluxConfigurationTests {
5454

5555
@Mock
@@ -67,7 +67,7 @@ public class DelegatingWebFluxConfigurationTests {
6767
private DelegatingWebFluxConfiguration delegatingConfig;
6868

6969

70-
@Before
70+
@BeforeEach
7171
public void setup() {
7272
delegatingConfig = new DelegatingWebFluxConfiguration();
7373
delegatingConfig.setApplicationContext(new StaticApplicationContext());

0 commit comments

Comments
 (0)