Skip to content

Commit cf31d08

Browse files
committed
Polish AssertJ support for MockMvc
See gh-21178
1 parent 4a74e1f commit cf31d08

31 files changed

+275
-257
lines changed

Diff for: spring-test/src/main/java/org/springframework/test/http/MediaTypeAssert.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@
3636
*/
3737
public class MediaTypeAssert extends AbstractObjectAssert<MediaTypeAssert, MediaType> {
3838

39+
public MediaTypeAssert(@Nullable String actual) {
40+
this(StringUtils.hasText(actual) ? MediaType.parseMediaType(actual) : null);
41+
}
42+
3943
public MediaTypeAssert(@Nullable MediaType mediaType) {
4044
super(mediaType, MediaTypeAssert.class);
4145
as("Media type");
4246
}
4347

44-
public MediaTypeAssert(@Nullable String actual) {
45-
this(StringUtils.hasText(actual) ? MediaType.parseMediaType(actual) : null);
46-
}
4748

4849
/**
4950
* Verify that the actual media type is equal to the given string
@@ -57,7 +58,8 @@ public MediaTypeAssert isEqualTo(String expected) {
5758
/**
5859
* Verify that the actual media type is
5960
* {@linkplain MediaType#isCompatibleWith(MediaType) compatible} with the
60-
* given one. Example: <pre><code class='java'>
61+
* given one.
62+
* <p>Example: <pre><code class='java'>
6163
* // Check that actual is compatible with "application/json"
6264
* assertThat(mediaType).isCompatibleWith(MediaType.APPLICATION_JSON);
6365
* </code></pre>
@@ -77,7 +79,8 @@ public MediaTypeAssert isCompatibleWith(MediaType mediaType) {
7779
/**
7880
* Verify that the actual media type is
7981
* {@linkplain MediaType#isCompatibleWith(MediaType) compatible} with the
80-
* given one. Example: <pre><code class='java'>
82+
* given one.
83+
* <p>Example: <pre><code class='java'>
8184
* // Check that actual is compatible with "text/plain"
8285
* assertThat(mediaType).isCompatibleWith("text/plain");
8386
* </code></pre>

Diff for: spring-test/src/main/java/org/springframework/test/json/AbstractJsonValueAssert.java

+27-22
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.test.json;
1818

19-
import java.lang.reflect.Array;
2019
import java.lang.reflect.Type;
2120
import java.util.List;
2221
import java.util.Map;
@@ -43,8 +42,9 @@
4342

4443
/**
4544
* Base AssertJ {@link org.assertj.core.api.Assert assertions} that can be
46-
* applied to a JSON value. In JSON, values must be one of the following data
47-
* types:
45+
* applied to a JSON value.
46+
*
47+
* <p>In JSON, values must be one of the following data types:
4848
* <ul>
4949
* <li>a {@linkplain #asString() string}</li>
5050
* <li>a {@linkplain #asNumber() number}</li>
@@ -53,7 +53,7 @@
5353
* <li>an {@linkplain #asMap() object} (JSON object)</li>
5454
* <li>{@linkplain #isNull() null}</li>
5555
* </ul>
56-
* This base class offers direct access for each of those types as well as a
56+
* This base class offers direct access for each of those types as well as
5757
* conversion methods based on an optional {@link GenericHttpMessageConverter}.
5858
*
5959
* @author Stephane Nicoll
@@ -71,12 +71,14 @@ public abstract class AbstractJsonValueAssert<SELF extends AbstractJsonValueAsse
7171

7272
protected AbstractJsonValueAssert(@Nullable Object actual, Class<?> selfType,
7373
@Nullable GenericHttpMessageConverter<Object> httpMessageConverter) {
74+
7475
super(actual, selfType);
7576
this.httpMessageConverter = httpMessageConverter;
7677
}
7778

79+
7880
/**
79-
* Verify that the actual value is a non-{@code null} {@link String}
81+
* Verify that the actual value is a non-{@code null} {@link String},
8082
* and return a new {@linkplain AbstractStringAssert assertion} object that
8183
* provides dedicated {@code String} assertions for it.
8284
*/
@@ -87,15 +89,15 @@ public AbstractStringAssert<?> asString() {
8789

8890
/**
8991
* Verify that the actual value is a non-{@code null} {@link Number},
90-
* usually an {@link Integer} or {@link Double} and return a new
92+
* usually an {@link Integer} or {@link Double}, and return a new
9193
* {@linkplain AbstractObjectAssert assertion} object for it.
9294
*/
9395
public AbstractObjectAssert<?, Number> asNumber() {
9496
return Assertions.assertThat(castTo(Number.class, "a number"));
9597
}
9698

9799
/**
98-
* Verify that the actual value is a non-{@code null} {@link Boolean}
100+
* Verify that the actual value is a non-{@code null} {@link Boolean},
99101
* and return a new {@linkplain AbstractBooleanAssert assertion} object
100102
* that provides dedicated {@code Boolean} assertions for it.
101103
*/
@@ -104,9 +106,9 @@ public AbstractBooleanAssert<?> asBoolean() {
104106
}
105107

106108
/**
107-
* Verify that the actual value is a non-{@code null} {@link Array}
108-
* and return a new {@linkplain ObjectArrayAssert assertion} object
109-
* that provides dedicated {@code Array} assertions for it.
109+
* Verify that the actual value is a non-{@code null} array, and return a
110+
* new {@linkplain ObjectArrayAssert assertion} object that provides dedicated
111+
* array assertions for it.
110112
*/
111113
public ObjectArrayAssert<Object> asArray() {
112114
List<?> list = castTo(List.class, "an array");
@@ -115,11 +117,12 @@ public ObjectArrayAssert<Object> asArray() {
115117
}
116118

117119
/**
118-
* Verify that the actual value is a non-{@code null} JSON object and
120+
* Verify that the actual value is a non-{@code null} JSON object, and
119121
* return a new {@linkplain AbstractMapAssert assertion} object that
120122
* provides dedicated assertions on individual elements of the
121-
* object. The returned map assertion object uses the attribute name as the
122-
* key, and the value can itself be any of the valid JSON values.
123+
* object.
124+
* <p>The returned map assertion object uses attribute names as the keys,
125+
* and the values can be any of the valid JSON values.
123126
*/
124127
@SuppressWarnings("unchecked")
125128
public AbstractMapAssert<?, Map<String, Object>, String, Object> asMap() {
@@ -138,7 +141,7 @@ private <T> T castTo(Class<T> expectedType, String description) {
138141

139142
/**
140143
* Verify that the actual value can be converted to an instance of the
141-
* given {@code target} and produce a new {@linkplain AbstractObjectAssert
144+
* given {@code target}, and produce a new {@linkplain AbstractObjectAssert
142145
* assertion} object narrowed to that type.
143146
* @param target the {@linkplain Class type} to convert the actual value to
144147
*/
@@ -150,7 +153,7 @@ public <T> AbstractObjectAssert<?, T> convertTo(Class<T> target) {
150153

151154
/**
152155
* Verify that the actual value can be converted to an instance of the
153-
* given {@code target} and produce a new {@linkplain AbstractObjectAssert
156+
* given {@code target}, and produce a new {@linkplain AbstractObjectAssert
154157
* assertion} object narrowed to that type.
155158
* @param target the {@linkplain ParameterizedTypeReference parameterized
156159
* type} to convert the actual value to
@@ -162,9 +165,10 @@ public <T> AbstractObjectAssert<?, T> convertTo(ParameterizedTypeReference<T> ta
162165
}
163166

164167
/**
165-
* Verify that the actual value is empty, that is a {@code null} scalar
166-
* value or an empty list or map. Can also be used when the path is using a
167-
* filter operator to validate that it dit not match.
168+
* Verify that the actual value is empty: either a {@code null} scalar value
169+
* or an empty list or map.
170+
* <p>Can also be used when the path uses a filter operator to validate that
171+
* it did not match.
168172
*/
169173
public SELF isEmpty() {
170174
if (!ObjectUtils.isEmpty(this.actual)) {
@@ -174,10 +178,10 @@ public SELF isEmpty() {
174178
}
175179

176180
/**
177-
* Verify that the actual value is not empty, that is a non-{@code null}
178-
* scalar value or a non-empty list or map. Can also be used when the path is
179-
* using a filter operator to validate that it dit match at least one
180-
* element.
181+
* Verify that the actual value is not empty: either a non-{@code null}
182+
* scalar value or a non-empty list or map.
183+
* <p>Can also be used when the path uses a filter operator to validate that
184+
* it did match at least one element.
181185
*/
182186
public SELF isNotEmpty() {
183187
if (ObjectUtils.isEmpty(this.actual)) {
@@ -225,6 +229,7 @@ private String actualToString() {
225229
return ObjectUtils.nullSafeToString(StringUtils.quoteIfString(this.actual));
226230
}
227231

232+
228233
private static final class ValueProcessingFailed extends BasicErrorMessageFactory {
229234

230235
private ValueProcessingFailed(String prefix, String actualToString, String errorMessage) {

Diff for: spring-test/src/main/java/org/springframework/test/json/JsonContent.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
import org.springframework.util.Assert;
2323

2424
/**
25-
* JSON content usually created from a JSON tester. Generally used only to
26-
* {@link AssertProvider provide} {@link JsonContentAssert} to AssertJ
27-
* {@code assertThat} calls.
25+
* JSON content which is generally used to {@link AssertProvider provide}
26+
* {@link JsonContentAssert} to AssertJ {@code assertThat} calls.
2827
*
2928
* @author Phillip Webb
3029
* @author Diego Berrueta
@@ -37,8 +36,9 @@ public final class JsonContent implements AssertProvider<JsonContentAssert> {
3736
@Nullable
3837
private final Class<?> resourceLoadClass;
3938

39+
4040
/**
41-
* Create a new {@link JsonContent} instance.
41+
* Create a new {@code JsonContent} instance.
4242
* @param json the actual JSON content
4343
* @param resourceLoadClass the source class used to load resources
4444
*/
@@ -48,6 +48,7 @@ public final class JsonContent implements AssertProvider<JsonContentAssert> {
4848
this.resourceLoadClass = resourceLoadClass;
4949
}
5050

51+
5152
/**
5253
* Use AssertJ's {@link org.assertj.core.api.Assertions#assertThat assertThat}
5354
* instead.

Diff for: spring-test/src/main/java/org/springframework/test/json/JsonContentAssert.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
/**
3939
* AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied
40-
* to a {@link CharSequence} representation of a json document, mostly to
41-
* compare the json document against a target, using {@linkplain JSONCompare
40+
* to a {@link CharSequence} representation of a JSON document, mostly to
41+
* compare the JSON document against a target, using {@linkplain JSONCompare
4242
* JSON Assert}.
4343
*
4444
* @author Phillip Webb
@@ -57,7 +57,7 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
5757
* relative to the given {@code resourceLoadClass}, using the given
5858
* {@code charset}.
5959
* @param json the actual JSON content
60-
* @param resourceLoadClass the source class used to load resources
60+
* @param resourceLoadClass the class used to load resources
6161
* @param charset the charset of the JSON resources
6262
*/
6363
public JsonContentAssert(@Nullable CharSequence json, @Nullable Class<?> resourceLoadClass,
@@ -71,7 +71,7 @@ public JsonContentAssert(@Nullable CharSequence json, @Nullable Class<?> resourc
7171
* Create a new {@link JsonContentAssert} instance that will load resources
7272
* relative to the given {@code resourceLoadClass}, using {@code UTF-8}.
7373
* @param json the actual JSON content
74-
* @param resourceLoadClass the source class used to load resources
74+
* @param resourceLoadClass the class used to load resources
7575
*/
7676
public JsonContentAssert(@Nullable CharSequence json, @Nullable Class<?> resourceLoadClass) {
7777
this(json, resourceLoadClass, null);
@@ -343,7 +343,6 @@ private JSONCompareResult compare(@Nullable CharSequence actualJson, @Nullable C
343343

344344
private JSONCompareResult compareForNull(@Nullable CharSequence expectedJson) {
345345
JSONCompareResult result = new JSONCompareResult();
346-
result.passed();
347346
if (expectedJson != null) {
348347
result.fail("Expected null JSON");
349348
}
@@ -352,14 +351,14 @@ private JSONCompareResult compareForNull(@Nullable CharSequence expectedJson) {
352351

353352
private JsonContentAssert assertNotFailed(JSONCompareResult result) {
354353
if (result.failed()) {
355-
failWithMessage("JSON Comparison failure: %s", result.getMessage());
354+
failWithMessage("JSON comparison failure: %s", result.getMessage());
356355
}
357356
return this;
358357
}
359358

360359
private JsonContentAssert assertNotPassed(JSONCompareResult result) {
361360
if (result.passed()) {
362-
failWithMessage("JSON Comparison failure: %s", result.getMessage());
361+
failWithMessage("JSON comparison failure: %s", result.getMessage());
363362
}
364363
return this;
365364
}

Diff for: spring-test/src/main/java/org/springframework/test/json/JsonLoader.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,23 @@ class JsonLoader {
4242

4343
private final Charset charset;
4444

45+
4546
JsonLoader(@Nullable Class<?> resourceLoadClass, @Nullable Charset charset) {
4647
this.resourceLoadClass = resourceLoadClass;
4748
this.charset = (charset != null ? charset : StandardCharsets.UTF_8);
4849
}
4950

51+
5052
@Nullable
5153
String getJson(@Nullable CharSequence source) {
5254
if (source == null) {
5355
return null;
5456
}
55-
if (source.toString().endsWith(".json")) {
56-
return getJson(new ClassPathResource(source.toString(), this.resourceLoadClass));
57+
String string = source.toString();
58+
if (string.endsWith(".json")) {
59+
return getJson(new ClassPathResource(string, this.resourceLoadClass));
5760
}
58-
return source.toString();
61+
return string;
5962
}
6063

6164
String getJson(Resource source) {

Diff for: spring-test/src/main/java/org/springframework/test/json/JsonPathAssert.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
/**
3333
* AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied
34-
* to a {@link CharSequence} representation of a json document using
34+
* to a {@link CharSequence} representation of a JSON document using
3535
* {@linkplain JsonPath JSON path}.
3636
*
3737
* @author Stephane Nicoll
@@ -41,17 +41,21 @@ public class JsonPathAssert extends AbstractAssert<JsonPathAssert, CharSequence>
4141

4242
private static final Failures failures = Failures.instance();
4343

44+
4445
@Nullable
4546
private final GenericHttpMessageConverter<Object> jsonMessageConverter;
4647

48+
4749
public JsonPathAssert(CharSequence json,
4850
@Nullable GenericHttpMessageConverter<Object> jsonMessageConverter) {
51+
4952
super(json, JsonPathAssert.class);
5053
this.jsonMessageConverter = jsonMessageConverter;
5154
}
5255

56+
5357
/**
54-
* Verify that the given JSON {@code path} is present and extract the JSON
58+
* Verify that the given JSON {@code path} is present, and extract the JSON
5559
* value for further {@linkplain JsonPathValueAssert assertions}.
5660
* @param path the {@link JsonPath} expression
5761
* @see #hasPathSatisfying(String, Consumer)
@@ -158,8 +162,9 @@ private JsonPathNotFound(String actual, String path) {
158162
static final class JsonPathNotExpected extends BasicErrorMessageFactory {
159163

160164
private JsonPathNotExpected(String actual, String path) {
161-
super("%nExpecting:%n %s%nTo not match JSON path:%n %s%n", actual, path);
165+
super("%nExpecting:%n %s%nNot to match JSON path:%n %s%n", actual, path);
162166
}
163167
}
164168
}
169+
165170
}

Diff for: spring-test/src/main/java/org/springframework/test/json/JsonPathValueAssert.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
* @author Stephane Nicoll
3030
* @since 6.2
3131
*/
32-
public class JsonPathValueAssert
33-
extends AbstractJsonValueAssert<JsonPathValueAssert> {
32+
public class JsonPathValueAssert extends AbstractJsonValueAssert<JsonPathValueAssert> {
3433

3534
private final String expression;
3635

3736

3837
JsonPathValueAssert(@Nullable Object actual, String expression,
3938
@Nullable GenericHttpMessageConverter<Object> httpMessageConverter) {
39+
4040
super(actual, JsonPathValueAssert.class, httpMessageConverter);
4141
this.expression = expression;
4242
}
@@ -45,4 +45,5 @@ public class JsonPathValueAssert
4545
protected String getExpectedErrorMessagePrefix() {
4646
return "Expected value at JSON path \"%s\":".formatted(this.expression);
4747
}
48+
4849
}

Diff for: spring-test/src/main/java/org/springframework/test/validation/AbstractBindingResultAssert.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ public abstract class AbstractBindingResultAssert<SELF extends AbstractBindingRe
4444

4545
private final String name;
4646

47+
4748
protected AbstractBindingResultAssert(String name, BindingResult bindingResult, Class<?> selfType) {
4849
super(bindingResult, selfType);
4950
this.name = name;
5051
as("Binding result for attribute '%s", this.name);
5152
}
5253

5354
/**
54-
* Verify that the total number of errors is equal to the given one.
55+
* Verify that the total number of errors is equal to the expected value.
5556
* @param expected the expected number of errors
5657
*/
5758
public SELF hasErrorsCount(int expected) {
@@ -73,7 +74,7 @@ public SELF hasFieldErrors(String... fieldNames) {
7374
/**
7475
* Verify that the actual binding result contains <em>only</em> fields in
7576
* error with the given {@code fieldNames}, and nothing else.
76-
* @param fieldNames the exhaustive list of field name that should be in error
77+
* @param fieldNames the exhaustive list of field names that should be in error
7778
*/
7879
public SELF hasOnlyFieldErrors(String... fieldNames) {
7980
assertThat(fieldErrorNames()).containsOnly(fieldNames);

Diff for: spring-test/src/main/java/org/springframework/test/web/UriAssert.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public UriAssert isEqualToTemplate(String uriTemplate, Object... uriVars) {
7070
* </code></pre>
7171
* @param uriPattern the pattern that is expected to match
7272
*/
73-
public UriAssert matchPattern(String uriPattern) {
73+
public UriAssert matchesPattern(String uriPattern) {
7474
Assertions.assertThat(pathMatcher.isPattern(uriPattern))
7575
.withFailMessage("'%s' is not an Ant-style path pattern", uriPattern).isTrue();
7676
Assertions.assertThat(pathMatcher.match(uriPattern, this.actual))

0 commit comments

Comments
 (0)