Migrate tests to JUnit 5

Closes gh-959
This commit is contained in:
Andy Wilkinson
2025-06-03 16:47:20 +01:00
parent d0c224de95
commit c7bde714d6
105 changed files with 4310 additions and 4163 deletions

View File

@@ -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.
@@ -19,8 +19,8 @@ package org.springframework.restdocs.restassured;
import io.restassured.RestAssured;
import io.restassured.specification.RequestSpecification;
import org.assertj.core.api.AbstractAssert;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
@@ -34,12 +34,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
public class RestAssuredParameterBehaviorTests {
class RestAssuredParameterBehaviorTests {
private static final MediaType APPLICATION_FORM_URLENCODED_ISO_8859_1 = MediaType
.parseMediaType(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=ISO-8859-1");
@ClassRule
@RegisterExtension
public static TomcatServer tomcat = new TomcatServer();
private final RestAssuredRequestConverter factory = new RestAssuredRequestConverter();
@@ -54,7 +54,7 @@ public class RestAssuredParameterBehaviorTests {
});
@Test
public void queryParameterOnGet() {
void queryParameterOnGet() {
this.spec.queryParam("a", "alpha", "apple")
.queryParam("b", "bravo")
.get("/query-parameter")
@@ -64,7 +64,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void queryParameterOnHead() {
void queryParameterOnHead() {
this.spec.queryParam("a", "alpha", "apple")
.queryParam("b", "bravo")
.head("/query-parameter")
@@ -74,7 +74,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void queryParameterOnPost() {
void queryParameterOnPost() {
this.spec.queryParam("a", "alpha", "apple")
.queryParam("b", "bravo")
.post("/query-parameter")
@@ -84,7 +84,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void queryParameterOnPut() {
void queryParameterOnPut() {
this.spec.queryParam("a", "alpha", "apple")
.queryParam("b", "bravo")
.put("/query-parameter")
@@ -94,7 +94,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void queryParameterOnPatch() {
void queryParameterOnPatch() {
this.spec.queryParam("a", "alpha", "apple")
.queryParam("b", "bravo")
.patch("/query-parameter")
@@ -104,7 +104,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void queryParameterOnDelete() {
void queryParameterOnDelete() {
this.spec.queryParam("a", "alpha", "apple")
.queryParam("b", "bravo")
.delete("/query-parameter")
@@ -114,7 +114,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void queryParameterOnOptions() {
void queryParameterOnOptions() {
this.spec.queryParam("a", "alpha", "apple")
.queryParam("b", "bravo")
.options("/query-parameter")
@@ -124,49 +124,49 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void paramOnGet() {
void paramOnGet() {
this.spec.param("a", "alpha", "apple").param("b", "bravo").get("/query-parameter").then().statusCode(200);
assertThatRequest(this.request).hasQueryParametersWithMethod(HttpMethod.GET);
}
@Test
public void paramOnHead() {
void paramOnHead() {
this.spec.param("a", "alpha", "apple").param("b", "bravo").head("/query-parameter").then().statusCode(200);
assertThatRequest(this.request).hasQueryParametersWithMethod(HttpMethod.HEAD);
}
@Test
public void paramOnPost() {
void paramOnPost() {
this.spec.param("a", "alpha", "apple").param("b", "bravo").post("/form-url-encoded").then().statusCode(200);
assertThatRequest(this.request).isFormUrlEncodedWithMethod(HttpMethod.POST);
}
@Test
public void paramOnPut() {
void paramOnPut() {
this.spec.param("a", "alpha", "apple").param("b", "bravo").put("/query-parameter").then().statusCode(200);
assertThatRequest(this.request).hasQueryParametersWithMethod(HttpMethod.PUT);
}
@Test
public void paramOnPatch() {
void paramOnPatch() {
this.spec.param("a", "alpha", "apple").param("b", "bravo").patch("/query-parameter").then().statusCode(200);
assertThatRequest(this.request).hasQueryParametersWithMethod(HttpMethod.PATCH);
}
@Test
public void paramOnDelete() {
void paramOnDelete() {
this.spec.param("a", "alpha", "apple").param("b", "bravo").delete("/query-parameter").then().statusCode(200);
assertThatRequest(this.request).hasQueryParametersWithMethod(HttpMethod.DELETE);
}
@Test
public void paramOnOptions() {
void paramOnOptions() {
this.spec.param("a", "alpha", "apple").param("b", "bravo").options("/query-parameter").then().statusCode(200);
assertThatRequest(this.request).hasQueryParametersWithMethod(HttpMethod.OPTIONS);
}
@Test
public void formParamOnGet() {
void formParamOnGet() {
this.spec.formParam("a", "alpha", "apple")
.formParam("b", "bravo")
.get("/query-parameter")
@@ -176,7 +176,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void formParamOnHead() {
void formParamOnHead() {
this.spec.formParam("a", "alpha", "apple")
.formParam("b", "bravo")
.head("/form-url-encoded")
@@ -186,7 +186,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void formParamOnPost() {
void formParamOnPost() {
this.spec.formParam("a", "alpha", "apple")
.formParam("b", "bravo")
.post("/form-url-encoded")
@@ -196,7 +196,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void formParamOnPut() {
void formParamOnPut() {
this.spec.formParam("a", "alpha", "apple")
.formParam("b", "bravo")
.put("/form-url-encoded")
@@ -206,7 +206,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void formParamOnPatch() {
void formParamOnPatch() {
this.spec.formParam("a", "alpha", "apple")
.formParam("b", "bravo")
.patch("/form-url-encoded")
@@ -216,7 +216,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void formParamOnDelete() {
void formParamOnDelete() {
this.spec.formParam("a", "alpha", "apple")
.formParam("b", "bravo")
.delete("/form-url-encoded")
@@ -226,7 +226,7 @@ public class RestAssuredParameterBehaviorTests {
}
@Test
public void formParamOnOptions() {
void formParamOnOptions() {
this.spec.formParam("a", "alpha", "apple")
.formParam("b", "bravo")
.options("/form-url-encoded")

View File

@@ -28,8 +28,8 @@ import java.util.Iterator;
import io.restassured.RestAssured;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.RequestSpecification;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
@@ -47,15 +47,15 @@ import static org.assertj.core.api.Assertions.entry;
*
* @author Andy Wilkinson
*/
public class RestAssuredRequestConverterTests {
class RestAssuredRequestConverterTests {
@ClassRule
@RegisterExtension
public static TomcatServer tomcat = new TomcatServer();
private final RestAssuredRequestConverter factory = new RestAssuredRequestConverter();
@Test
public void requestUri() {
void requestUri() {
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort());
requestSpec.get("/foo/bar");
OperationRequest request = this.factory.convert((FilterableRequestSpecification) requestSpec);
@@ -63,7 +63,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void requestMethod() {
void requestMethod() {
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort());
requestSpec.head("/foo/bar");
OperationRequest request = this.factory.convert((FilterableRequestSpecification) requestSpec);
@@ -71,7 +71,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void queryStringParameters() {
void queryStringParameters() {
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()).queryParam("foo", "bar");
requestSpec.get("/");
OperationRequest request = this.factory.convert((FilterableRequestSpecification) requestSpec);
@@ -79,7 +79,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void queryStringFromUrlParameters() {
void queryStringFromUrlParameters() {
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort());
requestSpec.get("/?foo=bar&foo=qix");
OperationRequest request = this.factory.convert((FilterableRequestSpecification) requestSpec);
@@ -87,7 +87,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void paramOnGetRequestIsMappedToQueryString() {
void paramOnGetRequestIsMappedToQueryString() {
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()).param("foo", "bar");
requestSpec.get("/");
OperationRequest request = this.factory.convert((FilterableRequestSpecification) requestSpec);
@@ -95,7 +95,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void headers() {
void headers() {
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()).header("Foo", "bar");
requestSpec.get("/");
OperationRequest request = this.factory.convert((FilterableRequestSpecification) requestSpec);
@@ -104,7 +104,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void headersWithCustomAccept() {
void headersWithCustomAccept() {
RequestSpecification requestSpec = RestAssured.given()
.port(tomcat.getPort())
.header("Foo", "bar")
@@ -117,7 +117,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void cookies() {
void cookies() {
RequestSpecification requestSpec = RestAssured.given()
.port(tomcat.getPort())
.cookie("cookie1", "cookieVal1")
@@ -138,7 +138,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void multipart() {
void multipart() {
RequestSpecification requestSpec = RestAssured.given()
.port(tomcat.getPort())
.multiPart("a", "a.txt", "alpha", null)
@@ -156,14 +156,14 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void byteArrayBody() {
void byteArrayBody() {
RequestSpecification requestSpec = RestAssured.given().body("body".getBytes()).port(tomcat.getPort());
requestSpec.post();
this.factory.convert((FilterableRequestSpecification) requestSpec);
}
@Test
public void stringBody() {
void stringBody() {
RequestSpecification requestSpec = RestAssured.given().body("body").port(tomcat.getPort());
requestSpec.post();
OperationRequest request = this.factory.convert((FilterableRequestSpecification) requestSpec);
@@ -171,7 +171,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void objectBody() {
void objectBody() {
RequestSpecification requestSpec = RestAssured.given().body(new ObjectBody("bar")).port(tomcat.getPort());
requestSpec.post();
OperationRequest request = this.factory.convert((FilterableRequestSpecification) requestSpec);
@@ -179,7 +179,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void byteArrayInputStreamBody() {
void byteArrayInputStreamBody() {
RequestSpecification requestSpec = RestAssured.given()
.body(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }))
.port(tomcat.getPort());
@@ -189,7 +189,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void fileBody() {
void fileBody() {
RequestSpecification requestSpec = RestAssured.given()
.body(new File("src/test/resources/body.txt"))
.port(tomcat.getPort());
@@ -199,7 +199,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void fileInputStreamBody() throws FileNotFoundException {
void fileInputStreamBody() throws FileNotFoundException {
FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt");
RequestSpecification requestSpec = RestAssured.given().body(inputStream).port(tomcat.getPort());
requestSpec.post();
@@ -209,7 +209,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void multipartWithByteArrayInputStreamBody() {
void multipartWithByteArrayInputStreamBody() {
RequestSpecification requestSpec = RestAssured.given()
.port(tomcat.getPort())
.multiPart("foo", "foo.txt", new ByteArrayInputStream("foo".getBytes()));
@@ -219,7 +219,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void multipartWithStringBody() {
void multipartWithStringBody() {
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()).multiPart("control", "foo");
requestSpec.post();
OperationRequest request = this.factory.convert((FilterableRequestSpecification) requestSpec);
@@ -227,7 +227,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void multipartWithByteArrayBody() {
void multipartWithByteArrayBody() {
RequestSpecification requestSpec = RestAssured.given()
.port(tomcat.getPort())
.multiPart("control", "file", "foo".getBytes());
@@ -237,7 +237,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void multipartWithFileBody() {
void multipartWithFileBody() {
RequestSpecification requestSpec = RestAssured.given()
.port(tomcat.getPort())
.multiPart(new File("src/test/resources/body.txt"));
@@ -247,7 +247,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void multipartWithFileInputStreamBody() throws FileNotFoundException {
void multipartWithFileInputStreamBody() throws FileNotFoundException {
FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt");
RequestSpecification requestSpec = RestAssured.given()
.port(tomcat.getPort())
@@ -259,7 +259,7 @@ public class RestAssuredRequestConverterTests {
}
@Test
public void multipartWithObjectBody() {
void multipartWithObjectBody() {
RequestSpecification requestSpec = RestAssured.given()
.port(tomcat.getPort())
.multiPart("control", new ObjectBody("bar"));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 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.
@@ -19,7 +19,7 @@ package org.springframework.restdocs.restassured;
import io.restassured.http.Headers;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatusCode;
import org.springframework.restdocs.operation.OperationResponse;
@@ -33,12 +33,12 @@ import static org.mockito.Mockito.mock;
*
* @author Andy Wilkinson
*/
public class RestAssuredResponseConverterTests {
class RestAssuredResponseConverterTests {
private final RestAssuredResponseConverter converter = new RestAssuredResponseConverter();
@Test
public void responseWithCustomStatus() {
void responseWithCustomStatus() {
Response response = mock(Response.class);
given(response.getStatusCode()).willReturn(600);
given(response.getHeaders()).willReturn(new Headers());

View File

@@ -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.
@@ -22,11 +22,13 @@ import java.util.Map;
import io.restassured.filter.FilterContext;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
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.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor;
import org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor;
@@ -45,10 +47,8 @@ import static org.mockito.Mockito.verify;
* @author Andy Wilkinson
* @author Filip Hrisafov
*/
public class RestAssuredRestDocumentationConfigurerTests {
@Rule
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
@ExtendWith(RestDocumentationExtension.class)
class RestAssuredRestDocumentationConfigurerTests {
private final FilterableRequestSpecification requestSpec = mock(FilterableRequestSpecification.class);
@@ -56,17 +56,21 @@ public class RestAssuredRestDocumentationConfigurerTests {
private final FilterContext filterContext = mock(FilterContext.class);
private final RestAssuredRestDocumentationConfigurer configurer = new RestAssuredRestDocumentationConfigurer(
this.restDocumentation);
private RestAssuredRestDocumentationConfigurer configurer;
@BeforeEach
void setUp(RestDocumentationContextProvider restDocumentation) {
this.configurer = new RestAssuredRestDocumentationConfigurer(restDocumentation);
}
@Test
public void nextFilterIsCalled() {
void nextFilterIsCalled() {
this.configurer.filter(this.requestSpec, this.responseSpec, this.filterContext);
verify(this.filterContext).next(this.requestSpec, this.responseSpec);
}
@Test
public void configurationIsAddedToTheContext() {
void configurationIsAddedToTheContext() {
this.configurer.operationPreprocessors()
.withRequestDefaults(Preprocessors.prettyPrint())
.withResponseDefaults(Preprocessors.modifyHeaders().remove("Foo"))

View File

@@ -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.
@@ -29,14 +29,15 @@ import java.util.regex.Pattern;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import org.assertj.core.api.Condition;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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;
@@ -80,18 +81,16 @@ import static org.springframework.restdocs.restassured.RestAssuredRestDocumentat
* @author Tomasz Kopczynski
* @author Filip Hrisafov
*/
public class RestAssuredRestDocumentationIntegrationTests {
@ExtendWith(RestDocumentationExtension.class)
class RestAssuredRestDocumentationIntegrationTests {
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
@ClassRule
public static TomcatServer tomcat = new TomcatServer();
@RegisterExtension
private static TomcatServer tomcat = new TomcatServer();
@Test
public void defaultSnippetGeneration() {
void defaultSnippetGeneration(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("default"))
.get("/")
.then()
@@ -101,10 +100,10 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void curlSnippetWithContent() {
void curlSnippetWithContent(RestDocumentationContextProvider restDocumentation) {
String contentType = "text/plain; charset=UTF-8";
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("curl-snippet-with-content"))
.accept("application/json")
.body("content")
@@ -120,10 +119,10 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void curlSnippetWithCookies() {
void curlSnippetWithCookies(RestDocumentationContextProvider restDocumentation) {
String contentType = "text/plain; charset=UTF-8";
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("curl-snippet-with-cookies"))
.accept("application/json")
.contentType(contentType)
@@ -138,9 +137,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void curlSnippetWithEmptyParameterQueryString() {
void curlSnippetWithEmptyParameterQueryString(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("curl-snippet-with-empty-parameter-query-string"))
.accept("application/json")
.param("a", "")
@@ -155,9 +154,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void curlSnippetWithQueryStringOnPost() {
void curlSnippetWithQueryStringOnPost(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("curl-snippet-with-query-string"))
.accept("application/json")
.param("foo", "bar")
@@ -174,9 +173,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void linksSnippet() {
void linksSnippet(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("links", links(linkWithRel("rel").description("The description"))))
.accept("application/json")
.get("/")
@@ -187,9 +186,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void pathParametersSnippet() {
void pathParametersSnippet(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("path-parameters",
pathParameters(parameterWithName("foo").description("The description"))))
.accept("application/json")
@@ -201,9 +200,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void queryParametersSnippet() {
void queryParametersSnippet(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("query-parameters",
queryParameters(parameterWithName("foo").description("The description"))))
.accept("application/json")
@@ -216,9 +215,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void requestFieldsSnippet() {
void requestFieldsSnippet(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("request-fields", requestFields(fieldWithPath("a").description("The description"))))
.accept("application/json")
.body("{\"a\":\"alpha\"}")
@@ -230,9 +229,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void requestPartsSnippet() {
void requestPartsSnippet(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("request-parts", requestParts(partWithName("a").description("The description"))))
.multiPart("a", "foo")
.post("/upload")
@@ -243,9 +242,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void responseFieldsSnippet() {
void responseFieldsSnippet(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("response-fields",
responseFields(fieldWithPath("a").description("The description"),
subsectionWithPath("links").description("Links to other resources"))))
@@ -258,9 +257,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void parameterizedOutputDirectory() {
void parameterizedOutputDirectory(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("{method-name}"))
.get("/")
.then()
@@ -270,9 +269,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void multiStep() {
void multiStep(RestDocumentationContextProvider restDocumentation) {
RequestSpecification spec = new RequestSpecBuilder().setPort(tomcat.getPort())
.addFilter(documentationConfiguration(this.restDocumentation))
.addFilter(documentationConfiguration(restDocumentation))
.addFilter(document("{method-name}-{step}"))
.build();
given(spec).get("/").then().statusCode(200);
@@ -287,10 +286,10 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void additionalSnippets() {
void additionalSnippets(RestDocumentationContextProvider restDocumentation) {
RestDocumentationFilter documentation = document("{method-name}-{step}");
RequestSpecification spec = new RequestSpecBuilder().setPort(tomcat.getPort())
.addFilter(documentationConfiguration(this.restDocumentation))
.addFilter(documentationConfiguration(restDocumentation))
.addFilter(documentation)
.build();
given(spec)
@@ -304,9 +303,9 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void responseWithCookie() {
void responseWithCookie(RestDocumentationContextProvider restDocumentation) {
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("set-cookie",
preprocessResponse(modifyHeaders().remove(HttpHeaders.DATE).remove(HttpHeaders.CONTENT_TYPE))))
.get("/set-cookie")
@@ -322,10 +321,10 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void preprocessedRequest() {
void preprocessedRequest(RestDocumentationContextProvider restDocumentation) {
Pattern pattern = Pattern.compile("(\"alpha\")");
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.header("a", "alpha")
.header("b", "bravo")
.contentType("application/json")
@@ -356,10 +355,10 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void defaultPreprocessedRequest() {
void defaultPreprocessedRequest(RestDocumentationContextProvider restDocumentation) {
Pattern pattern = Pattern.compile("(\"alpha\")");
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
.filter(documentationConfiguration(restDocumentation).operationPreprocessors()
.withRequestDefaults(prettyPrint(), replacePattern(pattern, "\"<<beta>>\""), modifyUris().removePort(),
modifyHeaders().remove("a").remove(HttpHeaders.CONTENT_LENGTH)))
.header("a", "alpha")
@@ -381,10 +380,10 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void preprocessedResponse() {
void preprocessedResponse(RestDocumentationContextProvider restDocumentation) {
Pattern pattern = Pattern.compile("(\"alpha\")");
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("original-response"))
.filter(document("preprocessed-response",
preprocessResponse(prettyPrint(), maskLinks(),
@@ -407,10 +406,10 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void defaultPreprocessedResponse() {
void defaultPreprocessedResponse(RestDocumentationContextProvider restDocumentation) {
Pattern pattern = Pattern.compile("(\"alpha\")");
given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
.filter(documentationConfiguration(restDocumentation).operationPreprocessors()
.withResponseDefaults(prettyPrint(), maskLinks(),
modifyHeaders().remove("a").remove("Transfer-Encoding").remove("Date").remove("Server"),
replacePattern(pattern, "\"<<beta>>\""),
@@ -432,7 +431,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void customSnippetTemplate() throws MalformedURLException {
void customSnippetTemplate(RestDocumentationContextProvider restDocumentation) throws MalformedURLException {
ClassLoader classLoader = new URLClassLoader(
new URL[] { new File("src/test/resources/custom-snippet-templates").toURI().toURL() },
getClass().getClassLoader());
@@ -441,7 +440,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
try {
given().port(tomcat.getPort())
.accept("application/json")
.filter(documentationConfiguration(this.restDocumentation))
.filter(documentationConfiguration(restDocumentation))
.filter(document("custom-snippet-template"))
.get("/")
.then()
@@ -455,7 +454,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void exceptionShouldBeThrownWhenCallDocumentRequestSpecificationNotConfigured() {
void exceptionShouldBeThrownWhenCallDocumentRequestSpecificationNotConfigured() {
assertThatThrownBy(() -> given().port(tomcat.getPort()).filter(document("default")).get("/"))
.isInstanceOf(IllegalStateException.class)
.hasMessage("REST Docs configuration not found. Did you forget to add a "
@@ -463,7 +462,7 @@ public class RestAssuredRestDocumentationIntegrationTests {
}
@Test
public void exceptionShouldBeThrownWhenCallDocumentSnippetsRequestSpecificationNotConfigured() {
void exceptionShouldBeThrownWhenCallDocumentSnippetsRequestSpecificationNotConfigured() {
RestDocumentationFilter documentation = document("{method-name}-{step}");
assertThatThrownBy(() -> given().port(tomcat.getPort())
.filter(documentation.document(responseHeaders(headerWithName("a").description("one"))))

View File

@@ -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.
@@ -32,46 +32,57 @@ import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.junit.rules.ExternalResource;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.springframework.http.MediaType;
import org.springframework.util.FileCopyUtils;
/**
* {@link ExternalResource} that starts and stops a Tomcat server.
* {@link Extension} that starts and stops a Tomcat server.
*
* @author Andy Wilkinson
*/
class TomcatServer extends ExternalResource {
private Tomcat tomcat;
class TomcatServer implements BeforeAllCallback, AfterAllCallback {
private int port;
@Override
protected void before() throws LifecycleException {
this.tomcat = new Tomcat();
this.tomcat.getConnector().setPort(0);
Context context = this.tomcat.addContext("/", null);
this.tomcat.addServlet("/", "test", new TestServlet());
context.addServletMappingDecoded("/", "test");
this.tomcat.addServlet("/", "set-cookie", new CookiesServlet());
context.addServletMappingDecoded("/set-cookie", "set-cookie");
this.tomcat.addServlet("/", "query-parameter", new QueryParameterServlet());
context.addServletMappingDecoded("/query-parameter", "query-parameter");
this.tomcat.addServlet("/", "form-url-encoded", new FormUrlEncodedServlet());
context.addServletMappingDecoded("/form-url-encoded", "form-url-encoded");
this.tomcat.start();
this.port = this.tomcat.getConnector().getLocalPort();
public void beforeAll(ExtensionContext extensionContext) {
Store store = extensionContext.getStore(Namespace.create(TomcatServer.class));
store.getOrComputeIfAbsent(Tomcat.class, (key) -> {
Tomcat tomcat = new Tomcat();
tomcat.getConnector().setPort(0);
Context context = tomcat.addContext("/", null);
tomcat.addServlet("/", "test", new TestServlet());
context.addServletMappingDecoded("/", "test");
tomcat.addServlet("/", "set-cookie", new CookiesServlet());
context.addServletMappingDecoded("/set-cookie", "set-cookie");
tomcat.addServlet("/", "query-parameter", new QueryParameterServlet());
context.addServletMappingDecoded("/query-parameter", "query-parameter");
tomcat.addServlet("/", "form-url-encoded", new FormUrlEncodedServlet());
context.addServletMappingDecoded("/form-url-encoded", "form-url-encoded");
try {
tomcat.start();
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
this.port = tomcat.getConnector().getLocalPort();
return tomcat;
});
}
@Override
protected void after() {
try {
this.tomcat.stop();
}
catch (LifecycleException ex) {
throw new RuntimeException(ex);
public void afterAll(ExtensionContext extensionContext) throws LifecycleException {
Store store = extensionContext.getStore(Namespace.create(TomcatServer.class));
Tomcat tomcat = store.get(Tomcat.class, Tomcat.class);
if (tomcat != null) {
tomcat.stop();
}
}