AssertionErrors.assertEquals exposes readable array representation

Issue: SPR-14281
This commit is contained in:
Juergen Hoeller 2016-05-30 17:16:03 +02:00
parent 86557f25af
commit 822e40e24f
3 changed files with 15 additions and 24 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.test.util; package org.springframework.test.util;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
@ -26,13 +27,8 @@ import org.springframework.util.ObjectUtils;
*/ */
public abstract class AssertionErrors { public abstract class AssertionErrors {
private AssertionErrors() {
}
/** /**
* Fails a test with the given message. * Fails a test with the given message.
*
* @param message describes the reason for the failure * @param message describes the reason for the failure
*/ */
public static void fail(String message) { public static void fail(String message) {
@ -42,7 +38,6 @@ public abstract class AssertionErrors {
/** /**
* Fails a test with the given message passing along expected and actual * Fails a test with the given message passing along expected and actual
* values to be added to the message. * values to be added to the message.
*
* <p>For example given: * <p>For example given:
* <pre class="code"> * <pre class="code">
* assertEquals("Response header [" + name + "]", actual, expected); * assertEquals("Response header [" + name + "]", actual, expected);
@ -51,7 +46,6 @@ public abstract class AssertionErrors {
* <pre class="code"> * <pre class="code">
* Response header [Accept] expected:&lt;application/json&gt; but was:&lt;text/plain&gt; * Response header [Accept] expected:&lt;application/json&gt; but was:&lt;text/plain&gt;
* </pre> * </pre>
*
* @param message describes the value that failed the match * @param message describes the value that failed the match
* @param expected expected value * @param expected expected value
* @param actual actual value * @param actual actual value
@ -63,7 +57,6 @@ public abstract class AssertionErrors {
/** /**
* Assert the given condition is {@code true} and raise an * Assert the given condition is {@code true} and raise an
* {@link AssertionError} if it is not. * {@link AssertionError} if it is not.
*
* @param message the message * @param message the message
* @param condition the condition to test for * @param condition the condition to test for
*/ */
@ -79,14 +72,13 @@ public abstract class AssertionErrors {
* <pre class="code"> * <pre class="code">
* assertEquals("Response header [" + name + "]", actual, expected); * assertEquals("Response header [" + name + "]", actual, expected);
* </pre> * </pre>
*
* @param message describes the value being checked * @param message describes the value being checked
* @param expected the expected value * @param expected the expected value
* @param actual the actual value * @param actual the actual value
*/ */
public static void assertEquals(String message, Object expected, Object actual) { public static void assertEquals(String message, Object expected, Object actual) {
if (!ObjectUtils.nullSafeEquals(expected, actual)) { if (!ObjectUtils.nullSafeEquals(expected, actual)) {
fail(message, expected, actual); fail(message, ObjectUtils.nullSafeToString(expected), ObjectUtils.nullSafeToString(actual));
} }
} }

View File

@ -38,7 +38,6 @@ import org.springframework.util.MultiValueMap;
import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.MatcherAssert.*;
import static org.springframework.test.util.AssertionErrors.*; import static org.springframework.test.util.AssertionErrors.*;
/** /**
* Factory for request content {@code RequestMatcher}'s. An instance of this * Factory for request content {@code RequestMatcher}'s. An instance of this
* class is typically accessed via {@link MockRestRequestMatchers#content()}. * class is typically accessed via {@link MockRestRequestMatchers#content()}.
@ -145,6 +144,7 @@ public class ContentRequestMatchers {
/** /**
* Parse the body as form data and compare to the given {@code MultiValueMap}. * Parse the body as form data and compare to the given {@code MultiValueMap}.
* @since 4.3
*/ */
public RequestMatcher formData(final MultiValueMap<String, String> expectedContent) { public RequestMatcher formData(final MultiValueMap<String, String> expectedContent) {
return new RequestMatcher() { return new RequestMatcher() {
@ -171,10 +171,8 @@ public class ContentRequestMatchers {
* Parse the request body and the given String as XML and assert that the * Parse the request body and the given String as XML and assert that the
* two are "similar" - i.e. they contain the same elements and attributes * two are "similar" - i.e. they contain the same elements and attributes
* regardless of order. * regardless of order.
*
* <p>Use of this matcher assumes the * <p>Use of this matcher assumes the
* <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available. * <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
*
* @param expectedXmlContent the expected XML content * @param expectedXmlContent the expected XML content
*/ */
public RequestMatcher xml(final String expectedXmlContent) { public RequestMatcher xml(final String expectedXmlContent) {
@ -211,6 +209,7 @@ public class ContentRequestMatchers {
}; };
} }
/** /**
* Abstract base class for XML {@link RequestMatcher}'s. * Abstract base class for XML {@link RequestMatcher}'s.
*/ */
@ -222,12 +221,13 @@ public class ContentRequestMatchers {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
matchInternal(mockRequest); matchInternal(mockRequest);
} }
catch (Exception e) { catch (Exception ex) {
throw new AssertionError("Failed to parse expected or actual XML request content: " + e.getMessage()); throw new AssertionError("Failed to parse expected or actual XML request content: " + ex.getMessage());
} }
} }
protected abstract void matchInternal(MockClientHttpRequest request) throws Exception; protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
} }
} }

View File

@ -27,7 +27,6 @@ import org.springframework.util.MultiValueMap;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
/** /**
* Unit tests for {@link ContentRequestMatchers}. * Unit tests for {@link ContentRequestMatchers}.
* *
@ -52,14 +51,14 @@ public class ContentRequestMatchersTests {
MockRestRequestMatchers.content().contentType(MediaType.APPLICATION_JSON).match(this.request); MockRestRequestMatchers.content().contentType(MediaType.APPLICATION_JSON).match(this.request);
} }
@Test(expected=AssertionError.class) @Test(expected = AssertionError.class)
public void testContentTypeNoMatch1() throws Exception { public void testContentTypeNoMatch1() throws Exception {
this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON); this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
MockRestRequestMatchers.content().contentType("application/xml").match(this.request); MockRestRequestMatchers.content().contentType("application/xml").match(this.request);
} }
@Test(expected=AssertionError.class) @Test(expected = AssertionError.class)
public void testContentTypeNoMatch2() throws Exception { public void testContentTypeNoMatch2() throws Exception {
this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON); this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
@ -73,7 +72,7 @@ public class ContentRequestMatchersTests {
MockRestRequestMatchers.content().string("test").match(this.request); MockRestRequestMatchers.content().string("test").match(this.request);
} }
@Test(expected=AssertionError.class) @Test(expected = AssertionError.class)
public void testStringNoMatch() throws Exception { public void testStringNoMatch() throws Exception {
this.request.getBody().write("test".getBytes()); this.request.getBody().write("test".getBytes());
@ -88,7 +87,7 @@ public class ContentRequestMatchersTests {
MockRestRequestMatchers.content().bytes(content).match(this.request); MockRestRequestMatchers.content().bytes(content).match(this.request);
} }
@Test(expected=AssertionError.class) @Test(expected = AssertionError.class)
public void testBytesNoMatch() throws Exception { public void testBytesNoMatch() throws Exception {
this.request.getBody().write("test".getBytes()); this.request.getBody().write("test".getBytes());
@ -119,7 +118,7 @@ public class ContentRequestMatchersTests {
MockRestRequestMatchers.content().xml(content).match(this.request); MockRestRequestMatchers.content().xml(content).match(this.request);
} }
@Test(expected=AssertionError.class) @Test(expected = AssertionError.class)
public void testXmlNoMatch() throws Exception { public void testXmlNoMatch() throws Exception {
this.request.getBody().write("<foo>11</foo>".getBytes()); this.request.getBody().write("<foo>11</foo>".getBytes());
@ -134,7 +133,7 @@ public class ContentRequestMatchersTests {
MockRestRequestMatchers.content().node(hasXPath("/foo/bar")).match(this.request); MockRestRequestMatchers.content().node(hasXPath("/foo/bar")).match(this.request);
} }
@Test(expected=AssertionError.class) @Test(expected = AssertionError.class)
public void testNodeMatcherNoMatch() throws Exception { public void testNodeMatcherNoMatch() throws Exception {
String content = "<foo><bar>baz</bar></foo>"; String content = "<foo><bar>baz</bar></foo>";
this.request.getBody().write(content.getBytes()); this.request.getBody().write(content.getBytes());