Skip to content

Commit 822e40e

Browse files
committed
AssertionErrors.assertEquals exposes readable array representation
Issue: SPR-14281
1 parent 86557f2 commit 822e40e

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.test.util;
1718

1819
import org.springframework.util.ObjectUtils;
@@ -26,13 +27,8 @@
2627
*/
2728
public abstract class AssertionErrors {
2829

29-
30-
private AssertionErrors() {
31-
}
32-
3330
/**
3431
* Fails a test with the given message.
35-
*
3632
* @param message describes the reason for the failure
3733
*/
3834
public static void fail(String message) {
@@ -42,7 +38,6 @@ public static void fail(String message) {
4238
/**
4339
* Fails a test with the given message passing along expected and actual
4440
* values to be added to the message.
45-
*
4641
* <p>For example given:
4742
* <pre class="code">
4843
* assertEquals("Response header [" + name + "]", actual, expected);
@@ -51,7 +46,6 @@ public static void fail(String message) {
5146
* <pre class="code">
5247
* Response header [Accept] expected:&lt;application/json&gt; but was:&lt;text/plain&gt;
5348
* </pre>
54-
*
5549
* @param message describes the value that failed the match
5650
* @param expected expected value
5751
* @param actual actual value
@@ -63,7 +57,6 @@ public static void fail(String message, Object expected, Object actual) {
6357
/**
6458
* Assert the given condition is {@code true} and raise an
6559
* {@link AssertionError} if it is not.
66-
*
6760
* @param message the message
6861
* @param condition the condition to test for
6962
*/
@@ -79,14 +72,13 @@ public static void assertTrue(String message, boolean condition) {
7972
* <pre class="code">
8073
* assertEquals("Response header [" + name + "]", actual, expected);
8174
* </pre>
82-
*
8375
* @param message describes the value being checked
8476
* @param expected the expected value
8577
* @param actual the actual value
8678
*/
8779
public static void assertEquals(String message, Object expected, Object actual) {
8880
if (!ObjectUtils.nullSafeEquals(expected, actual)) {
89-
fail(message, expected, actual);
81+
fail(message, ObjectUtils.nullSafeToString(expected), ObjectUtils.nullSafeToString(actual));
9082
}
9183
}
9284

spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import static org.hamcrest.MatcherAssert.*;
3939
import static org.springframework.test.util.AssertionErrors.*;
4040

41-
4241
/**
4342
* Factory for request content {@code RequestMatcher}'s. An instance of this
4443
* class is typically accessed via {@link MockRestRequestMatchers#content()}.
@@ -145,6 +144,7 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
145144

146145
/**
147146
* Parse the body as form data and compare to the given {@code MultiValueMap}.
147+
* @since 4.3
148148
*/
149149
public RequestMatcher formData(final MultiValueMap<String, String> expectedContent) {
150150
return new RequestMatcher() {
@@ -171,10 +171,8 @@ public HttpHeaders getHeaders() {
171171
* Parse the request body and the given String as XML and assert that the
172172
* two are "similar" - i.e. they contain the same elements and attributes
173173
* regardless of order.
174-
*
175174
* <p>Use of this matcher assumes the
176175
* <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
177-
*
178176
* @param expectedXmlContent the expected XML content
179177
*/
180178
public RequestMatcher xml(final String expectedXmlContent) {
@@ -211,6 +209,7 @@ protected void matchInternal(MockClientHttpRequest request) throws Exception {
211209
};
212210
}
213211

212+
214213
/**
215214
* Abstract base class for XML {@link RequestMatcher}'s.
216215
*/
@@ -222,12 +221,13 @@ public final void match(ClientHttpRequest request) throws IOException, Assertion
222221
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
223222
matchInternal(mockRequest);
224223
}
225-
catch (Exception e) {
226-
throw new AssertionError("Failed to parse expected or actual XML request content: " + e.getMessage());
224+
catch (Exception ex) {
225+
throw new AssertionError("Failed to parse expected or actual XML request content: " + ex.getMessage());
227226
}
228227
}
229228

230229
protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
231-
232230
}
231+
233232
}
233+

spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import static org.hamcrest.Matchers.*;
2929

30-
3130
/**
3231
* Unit tests for {@link ContentRequestMatchers}.
3332
*
@@ -52,14 +51,14 @@ public void testContentType() throws Exception {
5251
MockRestRequestMatchers.content().contentType(MediaType.APPLICATION_JSON).match(this.request);
5352
}
5453

55-
@Test(expected=AssertionError.class)
54+
@Test(expected = AssertionError.class)
5655
public void testContentTypeNoMatch1() throws Exception {
5756
this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
5857

5958
MockRestRequestMatchers.content().contentType("application/xml").match(this.request);
6059
}
6160

62-
@Test(expected=AssertionError.class)
61+
@Test(expected = AssertionError.class)
6362
public void testContentTypeNoMatch2() throws Exception {
6463
this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
6564

@@ -73,7 +72,7 @@ public void testString() throws Exception {
7372
MockRestRequestMatchers.content().string("test").match(this.request);
7473
}
7574

76-
@Test(expected=AssertionError.class)
75+
@Test(expected = AssertionError.class)
7776
public void testStringNoMatch() throws Exception {
7877
this.request.getBody().write("test".getBytes());
7978

@@ -88,7 +87,7 @@ public void testBytes() throws Exception {
8887
MockRestRequestMatchers.content().bytes(content).match(this.request);
8988
}
9089

91-
@Test(expected=AssertionError.class)
90+
@Test(expected = AssertionError.class)
9291
public void testBytesNoMatch() throws Exception {
9392
this.request.getBody().write("test".getBytes());
9493

@@ -119,7 +118,7 @@ public void testXml() throws Exception {
119118
MockRestRequestMatchers.content().xml(content).match(this.request);
120119
}
121120

122-
@Test(expected=AssertionError.class)
121+
@Test(expected = AssertionError.class)
123122
public void testXmlNoMatch() throws Exception {
124123
this.request.getBody().write("<foo>11</foo>".getBytes());
125124

@@ -134,7 +133,7 @@ public void testNodeMatcher() throws Exception {
134133
MockRestRequestMatchers.content().node(hasXPath("/foo/bar")).match(this.request);
135134
}
136135

137-
@Test(expected=AssertionError.class)
136+
@Test(expected = AssertionError.class)
138137
public void testNodeMatcherNoMatch() throws Exception {
139138
String content = "<foo><bar>baz</bar></foo>";
140139
this.request.getBody().write(content.getBytes());

0 commit comments

Comments
 (0)