diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java index 7544b315..02b694ce 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java @@ -20,9 +20,7 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -30,6 +28,7 @@ import org.springframework.http.MediaType; import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.operation.OperationResponseFactory; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -42,14 +41,10 @@ public class ContentTypeLinkExtractorTests { private final OperationResponseFactory responseFactory = new OperationResponseFactory(); - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test - public void extractionFailsWithNullContentType() throws IOException { - this.thrown.expect(IllegalStateException.class); - new ContentTypeLinkExtractor() - .extractLinks(this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), null)); + public void extractionFailsWithNullContentType() { + assertThatIllegalStateException().isThrownBy(() -> new ContentTypeLinkExtractor() + .extractLinks(this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), null))); } @Test diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java index f8f6c66f..017b4f97 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java @@ -25,13 +25,12 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.http.MediaType; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatThrownBy; /** @@ -41,9 +40,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; */ public class FieldPathPayloadSubsectionExtractorTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Test @SuppressWarnings("unchecked") public void extractMapSubsectionOfJsonMap() throws JsonParseException, JsonMappingException, IOException { @@ -100,27 +96,28 @@ public class FieldPathPayloadSubsectionExtractorTests { @Test public void extractMapSubsectionWithVaryingStructureFromMultiElementArrayInAJsonMap() { - this.thrown.expect(PayloadHandlingException.class); - this.thrown.expectMessage("The following non-optional uncommon paths were found: [a.[].b.d]"); - new FieldPathPayloadSubsectionExtractor("a.[].b").extractSubsection( - "{\"a\":[{\"b\":{\"c\":5}},{\"b\":{\"c\":6, \"d\": 7}}]}".getBytes(), MediaType.APPLICATION_JSON); + assertThatExceptionOfType(PayloadHandlingException.class) + .isThrownBy(() -> new FieldPathPayloadSubsectionExtractor("a.[].b").extractSubsection( + "{\"a\":[{\"b\":{\"c\":5}},{\"b\":{\"c\":6, \"d\": 7}}]}".getBytes(), + MediaType.APPLICATION_JSON)) + .withMessageContaining("The following non-optional uncommon paths were found: [a.[].b.d]"); } @Test public void extractMapSubsectionWithVaryingStructureFromInconsistentJsonMap() { - this.thrown.expect(PayloadHandlingException.class); - this.thrown.expectMessage("The following non-optional uncommon paths were found: [*.d, *.d.e, *.d.f]"); - new FieldPathPayloadSubsectionExtractor("*.d").extractSubsection( - "{\"a\":{\"b\":1},\"c\":{\"d\":{\"e\":1,\"f\":2}}}".getBytes(), MediaType.APPLICATION_JSON); + assertThatExceptionOfType(PayloadHandlingException.class) + .isThrownBy(() -> new FieldPathPayloadSubsectionExtractor("*.d").extractSubsection( + "{\"a\":{\"b\":1},\"c\":{\"d\":{\"e\":1,\"f\":2}}}".getBytes(), MediaType.APPLICATION_JSON)) + .withMessageContaining("The following non-optional uncommon paths were found: [*.d, *.d.e, *.d.f]"); } @Test public void extractMapSubsectionWithVaryingStructureFromInconsistentJsonMapWhereAllSubsectionFieldsAreOptional() { - this.thrown.expect(PayloadHandlingException.class); - this.thrown.expectMessage("The following non-optional uncommon paths were found: [*.d]"); - new FieldPathPayloadSubsectionExtractor("*.d").extractSubsection( - "{\"a\":{\"b\":1},\"c\":{\"d\":{\"e\":1,\"f\":2}}}".getBytes(), MediaType.APPLICATION_JSON, - Arrays.asList(new FieldDescriptor("e").optional(), new FieldDescriptor("f").optional())); + assertThatExceptionOfType(PayloadHandlingException.class) + .isThrownBy(() -> new FieldPathPayloadSubsectionExtractor("*.d").extractSubsection( + "{\"a\":{\"b\":1},\"c\":{\"d\":{\"e\":1,\"f\":2}}}".getBytes(), MediaType.APPLICATION_JSON, + Arrays.asList(new FieldDescriptor("e").optional(), new FieldDescriptor("f").optional()))) + .withMessageContaining("The following non-optional uncommon paths were found: [*.d]"); } @Test diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldTypeResolverTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldTypeResolverTests.java index 8ed7fbe1..044fd205 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldTypeResolverTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldTypeResolverTests.java @@ -18,13 +18,12 @@ package org.springframework.restdocs.payload; import java.util.Collections; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.http.MediaType; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link FieldTypeResolver}. @@ -33,9 +32,6 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class FieldTypeResolverTests { - @Rule - public ExpectedException thrownException = ExpectedException.none(); - @Test public void whenForContentWithDescriptorsCalledWithJsonContentThenReturnsJsonFieldTypeResolver() { assertThat(FieldTypeResolver.forContentWithDescriptors("{\"field\": \"value\"}".getBytes(), @@ -50,9 +46,8 @@ public class FieldTypeResolverTests { @Test public void whenForContentWithDescriptorsIsCalledWithInvalidContentThenExceptionIsThrown() { - this.thrownException.expect(PayloadHandlingException.class); - FieldTypeResolver.forContentWithDescriptors("some".getBytes(), MediaType.APPLICATION_XML, - Collections.emptyList()); + assertThatExceptionOfType(PayloadHandlingException.class).isThrownBy(() -> FieldTypeResolver + .forContentWithDescriptors("some".getBytes(), MediaType.APPLICATION_XML, Collections.emptyList())); } } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonContentHandlerTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonContentHandlerTests.java index d859fa4f..e512bca4 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonContentHandlerTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonContentHandlerTests.java @@ -20,11 +20,10 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link JsonContentHandler}. @@ -34,30 +33,28 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class JsonContentHandlerTests { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void typeForFieldWithNullValueMustMatch() { - this.thrown.expect(FieldTypesDoNotMatchException.class); FieldDescriptor descriptor = new FieldDescriptor("a").type(JsonFieldType.STRING); - new JsonContentHandler("{\"a\": null}".getBytes(), Arrays.asList(descriptor)).resolveFieldType(descriptor); + assertThatExceptionOfType(FieldTypesDoNotMatchException.class) + .isThrownBy(() -> new JsonContentHandler("{\"a\": null}".getBytes(), Arrays.asList(descriptor)) + .resolveFieldType(descriptor)); } @Test public void typeForFieldWithNotNullAndThenNullValueMustMatch() { - this.thrown.expect(FieldTypesDoNotMatchException.class); FieldDescriptor descriptor = new FieldDescriptor("a[].id").type(JsonFieldType.STRING); - new JsonContentHandler("{\"a\":[{\"id\":1},{\"id\":null}]}".getBytes(), Arrays.asList(descriptor)) - .resolveFieldType(descriptor); + assertThatExceptionOfType(FieldTypesDoNotMatchException.class).isThrownBy( + () -> new JsonContentHandler("{\"a\":[{\"id\":1},{\"id\":null}]}".getBytes(), Arrays.asList(descriptor)) + .resolveFieldType(descriptor)); } @Test public void typeForFieldWithNullAndThenNotNullValueMustMatch() { - this.thrown.expect(FieldTypesDoNotMatchException.class); FieldDescriptor descriptor = new FieldDescriptor("a.[].id").type(JsonFieldType.STRING); - new JsonContentHandler("{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes(), Arrays.asList(descriptor)) - .resolveFieldType(descriptor); + assertThatExceptionOfType(FieldTypesDoNotMatchException.class).isThrownBy( + () -> new JsonContentHandler("{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes(), Arrays.asList(descriptor)) + .resolveFieldType(descriptor)); } @Test @@ -111,8 +108,8 @@ public class JsonContentHandlerTests { @Test public void failsFastWithNonJsonContent() { - this.thrown.expect(PayloadHandlingException.class); - new JsonContentHandler("Non-JSON content".getBytes(), Collections.emptyList()); + assertThatExceptionOfType(PayloadHandlingException.class) + .isThrownBy(() -> new JsonContentHandler("Non-JSON content".getBytes(), Collections.emptyList())); } @Test diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldTypesDiscovererTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldTypesDiscovererTests.java index 5b39092d..1dc1e7b4 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldTypesDiscovererTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldTypesDiscovererTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 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,11 +19,10 @@ package org.springframework.restdocs.payload; import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link JsonFieldTypesDiscoverer}. @@ -34,9 +33,6 @@ public class JsonFieldTypesDiscovererTests { private final JsonFieldTypesDiscoverer fieldTypeDiscoverer = new JsonFieldTypesDiscoverer(); - @Rule - public ExpectedException thrownException = ExpectedException.none(); - @Test public void arrayField() throws IOException { assertThat(discoverFieldTypes("[]")).containsExactly(JsonFieldType.ARRAY); @@ -142,17 +138,17 @@ public class JsonFieldTypesDiscovererTests { } @Test - public void nonExistentSingleFieldProducesFieldDoesNotExistException() throws IOException { - this.thrownException.expect(FieldDoesNotExistException.class); - this.thrownException.expectMessage("The payload does not contain a field with the path 'a.b'"); - discoverFieldTypes("a.b", "{\"a\":{}}"); + public void nonExistentSingleFieldProducesFieldDoesNotExistException() { + assertThatExceptionOfType(FieldDoesNotExistException.class) + .isThrownBy(() -> discoverFieldTypes("a.b", "{\"a\":{}}")) + .withMessage("The payload does not contain a field with the path 'a.b'"); } @Test - public void nonExistentMultipleFieldsProducesFieldDoesNotExistException() throws IOException { - this.thrownException.expect(FieldDoesNotExistException.class); - this.thrownException.expectMessage("The payload does not contain a field with the path 'a[].b'"); - discoverFieldTypes("a[].b", "{\"a\":[{\"c\":1},{\"c\":2}]}"); + public void nonExistentMultipleFieldsProducesFieldDoesNotExistException() { + assertThatExceptionOfType(FieldDoesNotExistException.class) + .isThrownBy(() -> discoverFieldTypes("a[].b", "{\"a\":[{\"c\":1},{\"c\":2}]}")) + .withMessage("The payload does not contain a field with the path 'a[].b'"); } @Test diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/XmlContentHandlerTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/XmlContentHandlerTests.java index 24b1f8b2..5b03e4c1 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/XmlContentHandlerTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/XmlContentHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 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. @@ -20,11 +20,10 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath; @@ -35,9 +34,6 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.subsecti */ public class XmlContentHandlerTests { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void topLevelElementCanBeDocumented() { List descriptors = Arrays.asList(fieldWithPath("a").type("a").description("description")); @@ -84,8 +80,8 @@ public class XmlContentHandlerTests { @Test public void failsFastWithNonXmlContent() { - this.thrown.expect(PayloadHandlingException.class); - createHandler("non-XML content", Collections.emptyList()); + assertThatExceptionOfType(PayloadHandlingException.class) + .isThrownBy(() -> createHandler("non-XML content", Collections.emptyList())); } private XmlContentHandler createHandler(String xml, List descriptors) { diff --git a/spring-restdocs-platform/build.gradle b/spring-restdocs-platform/build.gradle index 6876deb6..2679ea20 100644 --- a/spring-restdocs-platform/build.gradle +++ b/spring-restdocs-platform/build.gradle @@ -11,7 +11,7 @@ dependencies { api("com.samskivert:jmustache:$jmustacheVersion") api("jakarta.servlet:jakarta.servlet-api:5.0.0") api("jakarta.validation:jakarta.validation-api:3.0.0") - api("junit:junit:4.12") + api("junit:junit:4.13.1") api("org.apache.pdfbox:pdfbox:2.0.27") api("org.asciidoctor:asciidoctorj:2.5.6") api("org.asciidoctor:asciidoctorj-pdf:2.3.0") diff --git a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java index ae8054b5..3d84d098 100644 --- a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java +++ b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java @@ -29,9 +29,7 @@ import io.restassured.RestAssured; import io.restassured.specification.FilterableRequestSpecification; import io.restassured.specification.RequestSpecification; import org.junit.ClassRule; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -41,6 +39,7 @@ import org.springframework.restdocs.operation.OperationRequestPart; import org.springframework.restdocs.operation.RequestCookie; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * Tests for {@link RestAssuredRequestConverter}. @@ -52,9 +51,6 @@ public class RestAssuredRequestConverterTests { @ClassRule public static TomcatServer tomcat = new TomcatServer(); - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private final RestAssuredRequestConverter factory = new RestAssuredRequestConverter(); @Test @@ -202,9 +198,9 @@ public class RestAssuredRequestConverterTests { FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt"); RequestSpecification requestSpec = RestAssured.given().body(inputStream).port(tomcat.getPort()); requestSpec.post(); - this.thrown.expect(IllegalStateException.class); - this.thrown.expectMessage("Cannot read content from input stream " + inputStream + " due to reset() failure"); - this.factory.convert((FilterableRequestSpecification) requestSpec); + assertThatIllegalStateException() + .isThrownBy(() -> this.factory.convert((FilterableRequestSpecification) requestSpec)) + .withMessage("Cannot read content from input stream " + inputStream + " due to reset() failure"); } @Test @@ -248,9 +244,9 @@ public class RestAssuredRequestConverterTests { RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()).multiPart("foo", "foo.txt", inputStream); requestSpec.post(); - this.thrown.expect(IllegalStateException.class); - this.thrown.expectMessage("Cannot read content from input stream " + inputStream + " due to reset() failure"); - this.factory.convert((FilterableRequestSpecification) requestSpec); + assertThatIllegalStateException() + .isThrownBy(() -> this.factory.convert((FilterableRequestSpecification) requestSpec)) + .withMessage("Cannot read content from input stream " + inputStream + " due to reset() failure"); } @Test