Skip to content

Commit 702c9ad

Browse files
committed
Upgrade to JUnit 4.13.1
Upgrade included dropping use of the now-deprecated ExpectedException in favor of AssertJ's assertions for thrown exceptions. Closes gh-863
1 parent ca777bd commit 702c9ad

File tree

8 files changed

+56
-84
lines changed

8 files changed

+56
-84
lines changed

Diff for: spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
import java.util.HashMap;
2121
import java.util.Map;
2222

23-
import org.junit.Rule;
2423
import org.junit.Test;
25-
import org.junit.rules.ExpectedException;
2624

2725
import org.springframework.http.HttpHeaders;
2826
import org.springframework.http.HttpStatus;
2927
import org.springframework.http.MediaType;
3028
import org.springframework.restdocs.operation.OperationResponse;
3129
import org.springframework.restdocs.operation.OperationResponseFactory;
3230

31+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3332
import static org.mockito.Mockito.mock;
3433
import static org.mockito.Mockito.verify;
3534

@@ -42,14 +41,10 @@ public class ContentTypeLinkExtractorTests {
4241

4342
private final OperationResponseFactory responseFactory = new OperationResponseFactory();
4443

45-
@Rule
46-
public ExpectedException thrown = ExpectedException.none();
47-
4844
@Test
49-
public void extractionFailsWithNullContentType() throws IOException {
50-
this.thrown.expect(IllegalStateException.class);
51-
new ContentTypeLinkExtractor()
52-
.extractLinks(this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), null));
45+
public void extractionFailsWithNullContentType() {
46+
assertThatIllegalStateException().isThrownBy(() -> new ContentTypeLinkExtractor()
47+
.extractLinks(this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), null)));
5348
}
5449

5550
@Test

Diff for: spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldPathPayloadSubsectionExtractorTests.java

+15-18
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@
2525
import com.fasterxml.jackson.databind.JsonMappingException;
2626
import com.fasterxml.jackson.databind.ObjectMapper;
2727
import com.fasterxml.jackson.databind.SerializationFeature;
28-
import org.junit.Rule;
2928
import org.junit.Test;
30-
import org.junit.rules.ExpectedException;
3129

3230
import org.springframework.http.MediaType;
3331

3432
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3534
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3635

3736
/**
@@ -41,9 +40,6 @@
4140
*/
4241
public class FieldPathPayloadSubsectionExtractorTests {
4342

44-
@Rule
45-
public final ExpectedException thrown = ExpectedException.none();
46-
4743
@Test
4844
@SuppressWarnings("unchecked")
4945
public void extractMapSubsectionOfJsonMap() throws JsonParseException, JsonMappingException, IOException {
@@ -100,27 +96,28 @@ public void extractMapSubsectionWithCommonStructureFromMultiElementArrayInAJsonM
10096

10197
@Test
10298
public void extractMapSubsectionWithVaryingStructureFromMultiElementArrayInAJsonMap() {
103-
this.thrown.expect(PayloadHandlingException.class);
104-
this.thrown.expectMessage("The following non-optional uncommon paths were found: [a.[].b.d]");
105-
new FieldPathPayloadSubsectionExtractor("a.[].b").extractSubsection(
106-
"{\"a\":[{\"b\":{\"c\":5}},{\"b\":{\"c\":6, \"d\": 7}}]}".getBytes(), MediaType.APPLICATION_JSON);
99+
assertThatExceptionOfType(PayloadHandlingException.class)
100+
.isThrownBy(() -> new FieldPathPayloadSubsectionExtractor("a.[].b").extractSubsection(
101+
"{\"a\":[{\"b\":{\"c\":5}},{\"b\":{\"c\":6, \"d\": 7}}]}".getBytes(),
102+
MediaType.APPLICATION_JSON))
103+
.withMessageContaining("The following non-optional uncommon paths were found: [a.[].b.d]");
107104
}
108105

109106
@Test
110107
public void extractMapSubsectionWithVaryingStructureFromInconsistentJsonMap() {
111-
this.thrown.expect(PayloadHandlingException.class);
112-
this.thrown.expectMessage("The following non-optional uncommon paths were found: [*.d, *.d.e, *.d.f]");
113-
new FieldPathPayloadSubsectionExtractor("*.d").extractSubsection(
114-
"{\"a\":{\"b\":1},\"c\":{\"d\":{\"e\":1,\"f\":2}}}".getBytes(), MediaType.APPLICATION_JSON);
108+
assertThatExceptionOfType(PayloadHandlingException.class)
109+
.isThrownBy(() -> new FieldPathPayloadSubsectionExtractor("*.d").extractSubsection(
110+
"{\"a\":{\"b\":1},\"c\":{\"d\":{\"e\":1,\"f\":2}}}".getBytes(), MediaType.APPLICATION_JSON))
111+
.withMessageContaining("The following non-optional uncommon paths were found: [*.d, *.d.e, *.d.f]");
115112
}
116113

117114
@Test
118115
public void extractMapSubsectionWithVaryingStructureFromInconsistentJsonMapWhereAllSubsectionFieldsAreOptional() {
119-
this.thrown.expect(PayloadHandlingException.class);
120-
this.thrown.expectMessage("The following non-optional uncommon paths were found: [*.d]");
121-
new FieldPathPayloadSubsectionExtractor("*.d").extractSubsection(
122-
"{\"a\":{\"b\":1},\"c\":{\"d\":{\"e\":1,\"f\":2}}}".getBytes(), MediaType.APPLICATION_JSON,
123-
Arrays.asList(new FieldDescriptor("e").optional(), new FieldDescriptor("f").optional()));
116+
assertThatExceptionOfType(PayloadHandlingException.class)
117+
.isThrownBy(() -> new FieldPathPayloadSubsectionExtractor("*.d").extractSubsection(
118+
"{\"a\":{\"b\":1},\"c\":{\"d\":{\"e\":1,\"f\":2}}}".getBytes(), MediaType.APPLICATION_JSON,
119+
Arrays.asList(new FieldDescriptor("e").optional(), new FieldDescriptor("f").optional())))
120+
.withMessageContaining("The following non-optional uncommon paths were found: [*.d]");
124121
}
125122

126123
@Test

Diff for: spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/FieldTypeResolverTests.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818

1919
import java.util.Collections;
2020

21-
import org.junit.Rule;
2221
import org.junit.Test;
23-
import org.junit.rules.ExpectedException;
2422

2523
import org.springframework.http.MediaType;
2624

2725
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2827

2928
/**
3029
* Tests for {@link FieldTypeResolver}.
@@ -33,9 +32,6 @@
3332
*/
3433
public class FieldTypeResolverTests {
3534

36-
@Rule
37-
public ExpectedException thrownException = ExpectedException.none();
38-
3935
@Test
4036
public void whenForContentWithDescriptorsCalledWithJsonContentThenReturnsJsonFieldTypeResolver() {
4137
assertThat(FieldTypeResolver.forContentWithDescriptors("{\"field\": \"value\"}".getBytes(),
@@ -50,9 +46,8 @@ public void whenForContentWithDescriptorsCalledWithXmlContentThenReturnsXmlConte
5046

5147
@Test
5248
public void whenForContentWithDescriptorsIsCalledWithInvalidContentThenExceptionIsThrown() {
53-
this.thrownException.expect(PayloadHandlingException.class);
54-
FieldTypeResolver.forContentWithDescriptors("some".getBytes(), MediaType.APPLICATION_XML,
55-
Collections.emptyList());
49+
assertThatExceptionOfType(PayloadHandlingException.class).isThrownBy(() -> FieldTypeResolver
50+
.forContentWithDescriptors("some".getBytes(), MediaType.APPLICATION_XML, Collections.emptyList()));
5651
}
5752

5853
}

Diff for: spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonContentHandlerTests.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23-
import org.junit.Rule;
2423
import org.junit.Test;
25-
import org.junit.rules.ExpectedException;
2624

2725
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2827

2928
/**
3029
* Tests for {@link JsonContentHandler}.
@@ -34,30 +33,28 @@
3433
*/
3534
public class JsonContentHandlerTests {
3635

37-
@Rule
38-
public ExpectedException thrown = ExpectedException.none();
39-
4036
@Test
4137
public void typeForFieldWithNullValueMustMatch() {
42-
this.thrown.expect(FieldTypesDoNotMatchException.class);
4338
FieldDescriptor descriptor = new FieldDescriptor("a").type(JsonFieldType.STRING);
44-
new JsonContentHandler("{\"a\": null}".getBytes(), Arrays.asList(descriptor)).resolveFieldType(descriptor);
39+
assertThatExceptionOfType(FieldTypesDoNotMatchException.class)
40+
.isThrownBy(() -> new JsonContentHandler("{\"a\": null}".getBytes(), Arrays.asList(descriptor))
41+
.resolveFieldType(descriptor));
4542
}
4643

4744
@Test
4845
public void typeForFieldWithNotNullAndThenNullValueMustMatch() {
49-
this.thrown.expect(FieldTypesDoNotMatchException.class);
5046
FieldDescriptor descriptor = new FieldDescriptor("a[].id").type(JsonFieldType.STRING);
51-
new JsonContentHandler("{\"a\":[{\"id\":1},{\"id\":null}]}".getBytes(), Arrays.asList(descriptor))
52-
.resolveFieldType(descriptor);
47+
assertThatExceptionOfType(FieldTypesDoNotMatchException.class).isThrownBy(
48+
() -> new JsonContentHandler("{\"a\":[{\"id\":1},{\"id\":null}]}".getBytes(), Arrays.asList(descriptor))
49+
.resolveFieldType(descriptor));
5350
}
5451

5552
@Test
5653
public void typeForFieldWithNullAndThenNotNullValueMustMatch() {
57-
this.thrown.expect(FieldTypesDoNotMatchException.class);
5854
FieldDescriptor descriptor = new FieldDescriptor("a.[].id").type(JsonFieldType.STRING);
59-
new JsonContentHandler("{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes(), Arrays.asList(descriptor))
60-
.resolveFieldType(descriptor);
55+
assertThatExceptionOfType(FieldTypesDoNotMatchException.class).isThrownBy(
56+
() -> new JsonContentHandler("{\"a\":[{\"id\":null},{\"id\":1}]}".getBytes(), Arrays.asList(descriptor))
57+
.resolveFieldType(descriptor));
6158
}
6259

6360
@Test
@@ -111,8 +108,8 @@ public void typeForFieldWithSometimesPresentOptionalAncestorCanBeProvidedExplici
111108

112109
@Test
113110
public void failsFastWithNonJsonContent() {
114-
this.thrown.expect(PayloadHandlingException.class);
115-
new JsonContentHandler("Non-JSON content".getBytes(), Collections.emptyList());
111+
assertThatExceptionOfType(PayloadHandlingException.class)
112+
.isThrownBy(() -> new JsonContentHandler("Non-JSON content".getBytes(), Collections.emptyList()));
116113
}
117114

118115
@Test

Diff for: spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/JsonFieldTypesDiscovererTests.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 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.
@@ -19,11 +19,10 @@
1919
import java.io.IOException;
2020

2121
import com.fasterxml.jackson.databind.ObjectMapper;
22-
import org.junit.Rule;
2322
import org.junit.Test;
24-
import org.junit.rules.ExpectedException;
2523

2624
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2726

2827
/**
2928
* Tests for {@link JsonFieldTypesDiscoverer}.
@@ -34,9 +33,6 @@ public class JsonFieldTypesDiscovererTests {
3433

3534
private final JsonFieldTypesDiscoverer fieldTypeDiscoverer = new JsonFieldTypesDiscoverer();
3635

37-
@Rule
38-
public ExpectedException thrownException = ExpectedException.none();
39-
4036
@Test
4137
public void arrayField() throws IOException {
4238
assertThat(discoverFieldTypes("[]")).containsExactly(JsonFieldType.ARRAY);
@@ -142,17 +138,17 @@ public void multipleFieldsThatAreAllNull() throws IOException {
142138
}
143139

144140
@Test
145-
public void nonExistentSingleFieldProducesFieldDoesNotExistException() throws IOException {
146-
this.thrownException.expect(FieldDoesNotExistException.class);
147-
this.thrownException.expectMessage("The payload does not contain a field with the path 'a.b'");
148-
discoverFieldTypes("a.b", "{\"a\":{}}");
141+
public void nonExistentSingleFieldProducesFieldDoesNotExistException() {
142+
assertThatExceptionOfType(FieldDoesNotExistException.class)
143+
.isThrownBy(() -> discoverFieldTypes("a.b", "{\"a\":{}}"))
144+
.withMessage("The payload does not contain a field with the path 'a.b'");
149145
}
150146

151147
@Test
152-
public void nonExistentMultipleFieldsProducesFieldDoesNotExistException() throws IOException {
153-
this.thrownException.expect(FieldDoesNotExistException.class);
154-
this.thrownException.expectMessage("The payload does not contain a field with the path 'a[].b'");
155-
discoverFieldTypes("a[].b", "{\"a\":[{\"c\":1},{\"c\":2}]}");
148+
public void nonExistentMultipleFieldsProducesFieldDoesNotExistException() {
149+
assertThatExceptionOfType(FieldDoesNotExistException.class)
150+
.isThrownBy(() -> discoverFieldTypes("a[].b", "{\"a\":[{\"c\":1},{\"c\":2}]}"))
151+
.withMessage("The payload does not contain a field with the path 'a[].b'");
156152
}
157153

158154
@Test

Diff for: spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/XmlContentHandlerTests.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 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.
@@ -20,11 +20,10 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23-
import org.junit.Rule;
2423
import org.junit.Test;
25-
import org.junit.rules.ExpectedException;
2624

2725
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2827
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
2928
import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath;
3029

@@ -35,9 +34,6 @@
3534
*/
3635
public class XmlContentHandlerTests {
3736

38-
@Rule
39-
public ExpectedException thrown = ExpectedException.none();
40-
4137
@Test
4238
public void topLevelElementCanBeDocumented() {
4339
List<FieldDescriptor> descriptors = Arrays.asList(fieldWithPath("a").type("a").description("description"));
@@ -84,8 +80,8 @@ public void multipleElementsCanBeInAscendingOrderDocumented() {
8480

8581
@Test
8682
public void failsFastWithNonXmlContent() {
87-
this.thrown.expect(PayloadHandlingException.class);
88-
createHandler("non-XML content", Collections.emptyList());
83+
assertThatExceptionOfType(PayloadHandlingException.class)
84+
.isThrownBy(() -> createHandler("non-XML content", Collections.emptyList()));
8985
}
9086

9187
private XmlContentHandler createHandler(String xml, List<FieldDescriptor> descriptors) {

Diff for: spring-restdocs-platform/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies {
1111
api("com.samskivert:jmustache:$jmustacheVersion")
1212
api("jakarta.servlet:jakarta.servlet-api:5.0.0")
1313
api("jakarta.validation:jakarta.validation-api:3.0.0")
14-
api("junit:junit:4.12")
14+
api("junit:junit:4.13.1")
1515
api("org.apache.pdfbox:pdfbox:2.0.27")
1616
api("org.asciidoctor:asciidoctorj:2.5.6")
1717
api("org.asciidoctor:asciidoctorj-pdf:2.3.0")

Diff for: spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
import io.restassured.specification.FilterableRequestSpecification;
3030
import io.restassured.specification.RequestSpecification;
3131
import org.junit.ClassRule;
32-
import org.junit.Rule;
3332
import org.junit.Test;
34-
import org.junit.rules.ExpectedException;
3533

3634
import org.springframework.http.HttpHeaders;
3735
import org.springframework.http.HttpMethod;
@@ -41,6 +39,7 @@
4139
import org.springframework.restdocs.operation.RequestCookie;
4240

4341
import static org.assertj.core.api.Assertions.assertThat;
42+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4443

4544
/**
4645
* Tests for {@link RestAssuredRequestConverter}.
@@ -52,9 +51,6 @@ public class RestAssuredRequestConverterTests {
5251
@ClassRule
5352
public static TomcatServer tomcat = new TomcatServer();
5453

55-
@Rule
56-
public final ExpectedException thrown = ExpectedException.none();
57-
5854
private final RestAssuredRequestConverter factory = new RestAssuredRequestConverter();
5955

6056
@Test
@@ -202,9 +198,9 @@ public void fileInputStreamBody() throws FileNotFoundException {
202198
FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt");
203199
RequestSpecification requestSpec = RestAssured.given().body(inputStream).port(tomcat.getPort());
204200
requestSpec.post();
205-
this.thrown.expect(IllegalStateException.class);
206-
this.thrown.expectMessage("Cannot read content from input stream " + inputStream + " due to reset() failure");
207-
this.factory.convert((FilterableRequestSpecification) requestSpec);
201+
assertThatIllegalStateException()
202+
.isThrownBy(() -> this.factory.convert((FilterableRequestSpecification) requestSpec))
203+
.withMessage("Cannot read content from input stream " + inputStream + " due to reset() failure");
208204
}
209205

210206
@Test
@@ -248,9 +244,9 @@ public void multipartWithFileInputStreamBody() throws FileNotFoundException {
248244
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()).multiPart("foo", "foo.txt",
249245
inputStream);
250246
requestSpec.post();
251-
this.thrown.expect(IllegalStateException.class);
252-
this.thrown.expectMessage("Cannot read content from input stream " + inputStream + " due to reset() failure");
253-
this.factory.convert((FilterableRequestSpecification) requestSpec);
247+
assertThatIllegalStateException()
248+
.isThrownBy(() -> this.factory.convert((FilterableRequestSpecification) requestSpec))
249+
.withMessage("Cannot read content from input stream " + inputStream + " due to reset() failure");
254250
}
255251

256252
@Test

0 commit comments

Comments
 (0)