Migrate tests to JUnit 5
Closes gh-959
This commit is contained in:
@@ -13,10 +13,10 @@ dependencies {
|
||||
internal(platform(project(":spring-restdocs-platform")))
|
||||
|
||||
testImplementation(testFixtures(project(":spring-restdocs-core")))
|
||||
testImplementation("junit:junit")
|
||||
testImplementation("org.assertj:assertj-core")
|
||||
testImplementation("org.hamcrest:hamcrest-library")
|
||||
testImplementation("org.mockito:mockito-core")
|
||||
|
||||
testRuntimeOnly("org.springframework:spring-context")
|
||||
}
|
||||
|
||||
tasks.named("test") {
|
||||
useJUnitPlatform();
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
@@ -48,12 +48,12 @@ import static org.springframework.web.reactive.function.server.RequestPredicates
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class WebTestClientRequestConverterTests {
|
||||
class WebTestClientRequestConverterTests {
|
||||
|
||||
private final WebTestClientRequestConverter converter = new WebTestClientRequestConverter();
|
||||
|
||||
@Test
|
||||
public void httpRequest() {
|
||||
void httpRequest() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
@@ -69,7 +69,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpRequestWithCustomPort() {
|
||||
void httpRequestWithCustomPort() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost:8080")
|
||||
@@ -85,7 +85,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestWithHeaders() {
|
||||
void requestWithHeaders() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/"), (req) -> null))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
@@ -105,7 +105,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpsRequest() {
|
||||
void httpsRequest() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient()
|
||||
.baseUrl("https://localhost")
|
||||
@@ -121,7 +121,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpsRequestWithCustomPort() {
|
||||
void httpsRequestWithCustomPort() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient()
|
||||
.baseUrl("https://localhost:8443")
|
||||
@@ -137,7 +137,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestWithQueryString() {
|
||||
void getRequestWithQueryString() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
@@ -153,7 +153,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postRequestWithFormDataParameters() {
|
||||
void postRequestWithFormDataParameters() {
|
||||
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
|
||||
parameters.addAll("a", Arrays.asList("alpha", "apple"));
|
||||
parameters.addAll("b", Arrays.asList("br&vo"));
|
||||
@@ -181,7 +181,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postRequestWithQueryStringParameters() {
|
||||
void postRequestWithQueryStringParameters() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> {
|
||||
req.body(BodyExtractors.toFormData()).block();
|
||||
return null;
|
||||
@@ -200,7 +200,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postRequestWithQueryStringAndFormDataParameters() {
|
||||
void postRequestWithQueryStringAndFormDataParameters() {
|
||||
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
|
||||
parameters.addAll("a", Arrays.asList("apple"));
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> {
|
||||
@@ -227,7 +227,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postRequestWithNoContentType() {
|
||||
void postRequestWithNoContentType() {
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> ServerResponse.ok().build()))
|
||||
.configureClient()
|
||||
@@ -244,7 +244,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartUpload() {
|
||||
void multipartUpload() {
|
||||
MultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>();
|
||||
multipartData.add("file", new byte[] { 1, 2, 3, 4 });
|
||||
ExchangeResult result = WebTestClient
|
||||
@@ -274,7 +274,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartUploadFromResource() {
|
||||
void multipartUploadFromResource() {
|
||||
MultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>();
|
||||
multipartData.add("file", new ByteArrayResource(new byte[] { 1, 2, 3, 4 }) {
|
||||
|
||||
@@ -314,7 +314,7 @@ public class WebTestClientRequestConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestWithCookies() {
|
||||
void requestWithCookies() {
|
||||
ExchangeResult result = WebTestClient.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null))
|
||||
.configureClient()
|
||||
.baseUrl("http://localhost")
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.restdocs.webtestclient;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -40,12 +40,12 @@ import static org.springframework.web.reactive.function.server.RequestPredicates
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class WebTestClientResponseConverterTests {
|
||||
class WebTestClientResponseConverterTests {
|
||||
|
||||
private final WebTestClientResponseConverter converter = new WebTestClientResponseConverter();
|
||||
|
||||
@Test
|
||||
public void basicResponse() {
|
||||
void basicResponse() {
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(
|
||||
RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.ok().bodyValue("Hello, World!")))
|
||||
@@ -66,7 +66,7 @@ public class WebTestClientResponseConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseWithCookie() {
|
||||
void responseWithCookie() {
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(RouterFunctions.route(GET("/foo"),
|
||||
(req) -> ServerResponse.ok()
|
||||
@@ -92,7 +92,7 @@ public class WebTestClientResponseConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseWithNonStandardStatusCode() {
|
||||
void responseWithNonStandardStatusCode() {
|
||||
ExchangeResult result = WebTestClient
|
||||
.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.status(210).build()))
|
||||
.configureClient()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2023 the original author or authors.
|
||||
* Copyright 2014-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,12 +18,14 @@ package org.springframework.restdocs.webtestclient;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.restdocs.JUnitRestDocumentation;
|
||||
import org.springframework.restdocs.RestDocumentationContextProvider;
|
||||
import org.springframework.restdocs.RestDocumentationExtension;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||
import org.springframework.web.reactive.function.client.ExchangeFunction;
|
||||
@@ -38,16 +40,19 @@ import static org.mockito.Mockito.verify;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class WebTestClientRestDocumentationConfigurerTests {
|
||||
@ExtendWith(RestDocumentationExtension.class)
|
||||
class WebTestClientRestDocumentationConfigurerTests {
|
||||
|
||||
@Rule
|
||||
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
|
||||
private WebTestClientRestDocumentationConfigurer configurer;
|
||||
|
||||
private final WebTestClientRestDocumentationConfigurer configurer = new WebTestClientRestDocumentationConfigurer(
|
||||
this.restDocumentation);
|
||||
@BeforeEach
|
||||
void setUp(RestDocumentationContextProvider restDocumentation) {
|
||||
this.configurer = new WebTestClientRestDocumentationConfigurer(restDocumentation);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configurationCanBeRetrievedButOnlyOnce() {
|
||||
void configurationCanBeRetrievedButOnlyOnce() {
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("/test"))
|
||||
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1")
|
||||
.build();
|
||||
@@ -58,7 +63,7 @@ public class WebTestClientRestDocumentationConfigurerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestUriHasDefaultsAppliedWhenItHasNoHost() {
|
||||
void requestUriHasDefaultsAppliedWhenItHasNoHost() {
|
||||
ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("/test?foo=bar#baz"))
|
||||
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1")
|
||||
.build();
|
||||
@@ -70,7 +75,7 @@ public class WebTestClientRestDocumentationConfigurerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestUriIsNotChangedWhenItHasAHost() {
|
||||
void requestUriIsNotChangedWhenItHasAHost() {
|
||||
ClientRequest request = ClientRequest
|
||||
.create(HttpMethod.GET, URI.create("https://api.example.com:4567/test?foo=bar#baz"))
|
||||
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2023 the original author or authors.
|
||||
* Copyright 2014-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,15 +30,16 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.restdocs.JUnitRestDocumentation;
|
||||
import org.springframework.restdocs.RestDocumentationContextProvider;
|
||||
import org.springframework.restdocs.RestDocumentationExtension;
|
||||
import org.springframework.restdocs.templates.TemplateFormat;
|
||||
import org.springframework.restdocs.templates.TemplateFormats;
|
||||
import org.springframework.restdocs.testfixtures.SnippetConditions;
|
||||
@@ -75,15 +76,13 @@ import static org.springframework.web.reactive.function.BodyInserters.fromValue;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@ExtendWith(RestDocumentationExtension.class)
|
||||
public class WebTestClientRestDocumentationIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
|
||||
|
||||
private WebTestClient webTestClient;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@BeforeEach
|
||||
void setUp(RestDocumentationContextProvider restDocumentation) {
|
||||
RouterFunction<ServerResponse> route = RouterFunctions
|
||||
.route(RequestPredicates.GET("/"),
|
||||
(request) -> ServerResponse.status(HttpStatus.OK).body(fromValue(new Person("Jane", "Doe"))))
|
||||
@@ -99,12 +98,12 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
this.webTestClient = WebTestClient.bindToRouterFunction(route)
|
||||
.configureClient()
|
||||
.baseUrl("https://api.example.com")
|
||||
.filter(documentationConfiguration(this.restDocumentation))
|
||||
.filter(documentationConfiguration(restDocumentation))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultSnippetGeneration() {
|
||||
void defaultSnippetGeneration() {
|
||||
File outputDir = new File("build/generated-snippets/default-snippets");
|
||||
FileSystemUtils.deleteRecursively(outputDir);
|
||||
this.webTestClient.get()
|
||||
@@ -119,7 +118,7 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathParametersSnippet() {
|
||||
void pathParametersSnippet() {
|
||||
this.webTestClient.get()
|
||||
.uri("/{foo}/{bar}", "1", "2")
|
||||
.exchange()
|
||||
@@ -136,7 +135,7 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParametersSnippet() {
|
||||
void queryParametersSnippet() {
|
||||
this.webTestClient.get()
|
||||
.uri("/?a=alpha&b=bravo")
|
||||
.exchange()
|
||||
@@ -153,7 +152,7 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipart() {
|
||||
void multipart() {
|
||||
MultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>();
|
||||
multipartData.add("a", "alpha");
|
||||
multipartData.add("b", "bravo");
|
||||
@@ -173,7 +172,7 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseWithSetCookie() {
|
||||
void responseWithSetCookie() {
|
||||
this.webTestClient.get()
|
||||
.uri("/set-cookie")
|
||||
.exchange()
|
||||
@@ -187,7 +186,7 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void curlSnippetWithCookies() {
|
||||
void curlSnippetWithCookies() {
|
||||
this.webTestClient.get()
|
||||
.uri("/")
|
||||
.cookie("cookieName", "cookieVal")
|
||||
@@ -204,7 +203,7 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void curlSnippetWithEmptyParameterQueryString() {
|
||||
void curlSnippetWithEmptyParameterQueryString() {
|
||||
this.webTestClient.get()
|
||||
.uri("/?a=")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
@@ -220,7 +219,7 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpieSnippetWithCookies() {
|
||||
void httpieSnippetWithCookies() {
|
||||
this.webTestClient.get()
|
||||
.uri("/")
|
||||
.cookie("cookieName", "cookieVal")
|
||||
@@ -237,7 +236,7 @@ public class WebTestClientRestDocumentationIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void illegalStateExceptionShouldBeThrownWhenCallDocumentWebClientNotConfigured() {
|
||||
void illegalStateExceptionShouldBeThrownWhenCallDocumentWebClientNotConfigured() {
|
||||
assertThatThrownBy(() -> this.webTestClient
|
||||
.mutateWith((builder, httpHandlerBuilder, connector) -> builder.filters(List::clear).build())
|
||||
.get()
|
||||
|
||||
Reference in New Issue
Block a user