18
18
19
19
import java .nio .charset .Charset ;
20
20
21
+ import org .assertj .core .api .AbstractByteArrayAssert ;
22
+ import org .assertj .core .api .AbstractStringAssert ;
23
+ import org .assertj .core .api .Assertions ;
24
+ import org .assertj .core .api .ByteArrayAssert ;
25
+
21
26
import org .springframework .http .converter .GenericHttpMessageConverter ;
22
27
import org .springframework .lang .Nullable ;
23
28
import org .springframework .mock .web .MockHttpServletResponse ;
29
+ import org .springframework .test .json .AbstractJsonContentAssert ;
30
+ import org .springframework .test .json .JsonContentAssert ;
24
31
import org .springframework .test .web .UriAssert ;
25
32
26
33
/**
@@ -45,22 +52,62 @@ protected AbstractMockHttpServletResponseAssert(
45
52
this .jsonMessageConverter = jsonMessageConverter ;
46
53
}
47
54
55
+
48
56
/**
49
- * Return a new {@linkplain ResponseBodyAssert assertion} object that uses
50
- * the response body as the object to test. The returned assertion object
51
- * provides access to the raw byte array, a String value decoded using the
52
- * response's character encoding, and dedicated JSON testing support.
57
+ * Return a new {@linkplain AbstractStringAssert assertion} object that uses
58
+ * the response body converted to text as the object to test.
53
59
* <p>Examples: <pre><code class='java'>
54
60
* // Check that the response body is equal to "Hello World":
55
- * assertThat(response).body().isEqualTo("Hello World");
61
+ * assertThat(response).bodyText().isEqualTo("Hello World");
62
+ * </code></pre>
63
+ */
64
+ public AbstractStringAssert <?> bodyText () {
65
+ return Assertions .assertThat (readBody ());
66
+ }
67
+
68
+ /**
69
+ * Return a new {@linkplain AbstractJsonContentAssert assertion} object that
70
+ * uses the response body converted to text as the object to test. Compared
71
+ * to {@link #bodyText()}, the assertion object provides dedicated JSON
72
+ * support.
73
+ * <p>Examples: <pre><code class='java'>
74
+ * // Check that the response body is strictly equal to the content of
75
+ * // "/com/acme/sample/person-created.json":
76
+ * assertThat(response).bodyJson()
77
+ * .isStrictlyEqualToJson("/com/acme/sample/person-created.json");
78
+ *
79
+ * // Check that the response is strictly equal to the content of the
80
+ * // specified file located in the same package as the PersonController:
81
+ * assertThat(response).bodyJson().withResourceLoadClass(PersonController.class)
82
+ * .isStrictlyEqualToJson("person-created.json");
83
+ * </code></pre>
84
+ * The returned assert object also supports JSON path expressions.
85
+ * <p>Examples: <pre><code class='java'>
86
+ * // Check that the JSON document does not have an "error" element
87
+ * assertThat(response).bodyJson().doesNotHavePath("$.error");
56
88
*
57
- * // Check that the response body is strictly equal to the content of "test.json":
58
- * assertThat(response).body().json().isStrictlyEqualToJson("test.json");
89
+ * // Check that the JSON document as a top level "message" element
90
+ * assertThat(response).bodyJson()
91
+ * .extractingPath("$.message").asString().isEqualTo("hello");
59
92
* </code></pre>
60
93
*/
61
- public ResponseBodyAssert body () {
62
- return new ResponseBodyAssert (getResponse ().getContentAsByteArray (),
63
- Charset .forName (getResponse ().getCharacterEncoding ()), this .jsonMessageConverter );
94
+ public AbstractJsonContentAssert <?> bodyJson () {
95
+ return new JsonContentAssert (readBody (), this .jsonMessageConverter );
96
+ }
97
+
98
+ private String readBody () {
99
+ return new String (getResponse ().getContentAsByteArray (),
100
+ Charset .forName (getResponse ().getCharacterEncoding ()));
101
+ }
102
+
103
+ /**
104
+ * Return a new {@linkplain AbstractByteArrayAssert assertion} object that
105
+ * uses the response body as the object to test.
106
+ * @see #bodyText()
107
+ * @see #bodyJson()
108
+ */
109
+ public AbstractByteArrayAssert <?> body () {
110
+ return new ByteArrayAssert (getResponse ().getContentAsByteArray ());
64
111
}
65
112
66
113
/**
@@ -89,6 +136,14 @@ public UriAssert redirectedUrl() {
89
136
return new UriAssert (getResponse ().getRedirectedUrl (), "Redirected URL" );
90
137
}
91
138
139
+ /**
140
+ * Verify that the response body is equal to the given value.
141
+ */
142
+ public SELF hasBodyTextEqualTo (String bodyText ) {
143
+ bodyText ().isEqualTo (bodyText );
144
+ return this .myself ;
145
+ }
146
+
92
147
/**
93
148
* Verify that the forwarded URL is equal to the given value.
94
149
* @param forwardedUrl the expected forwarded URL (can be null)
0 commit comments