AssertionErrors.assertEquals exposes readable array representation

Issue: SPR-14281
master
Juergen Hoeller 8 years ago
parent 86557f25af
commit 822e40e24f
  1. 14
      spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java
  2. 12
      spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java
  3. 13
      spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java

@ -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");
* 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
* limitations under the License.
*/
package org.springframework.test.util;
import org.springframework.util.ObjectUtils;
@ -26,13 +27,8 @@ import org.springframework.util.ObjectUtils;
*/
public abstract class AssertionErrors {
private AssertionErrors() {
}
/**
* Fails a test with the given message.
*
* @param message describes the reason for the failure
*/
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
* values to be added to the message.
*
* <p>For example given:
* <pre class="code">
* assertEquals("Response header [" + name + "]", actual, expected);
@ -51,7 +46,6 @@ public abstract class AssertionErrors {
* <pre class="code">
* Response header [Accept] expected:&lt;application/json&gt; but was:&lt;text/plain&gt;
* </pre>
*
* @param message describes the value that failed the match
* @param expected expected value
* @param actual actual value
@ -63,7 +57,6 @@ public abstract class AssertionErrors {
/**
* Assert the given condition is {@code true} and raise an
* {@link AssertionError} if it is not.
*
* @param message the message
* @param condition the condition to test for
*/
@ -79,14 +72,13 @@ public abstract class AssertionErrors {
* <pre class="code">
* assertEquals("Response header [" + name + "]", actual, expected);
* </pre>
*
* @param message describes the value being checked
* @param expected the expected value
* @param actual the actual value
*/
public static void assertEquals(String message, Object expected, Object actual) {
if (!ObjectUtils.nullSafeEquals(expected, actual)) {
fail(message, expected, actual);
fail(message, ObjectUtils.nullSafeToString(expected), ObjectUtils.nullSafeToString(actual));
}
}

@ -38,7 +38,6 @@ import org.springframework.util.MultiValueMap;
import static org.hamcrest.MatcherAssert.*;
import static org.springframework.test.util.AssertionErrors.*;
/**
* Factory for request content {@code RequestMatcher}'s. An instance of this
* 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}.
* @since 4.3
*/
public RequestMatcher formData(final MultiValueMap<String, String> expectedContent) {
return new RequestMatcher() {
@ -171,10 +171,8 @@ public class ContentRequestMatchers {
* 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
* regardless of order.
*
* <p>Use of this matcher assumes the
* <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
*
* @param expectedXmlContent the expected XML content
*/
public RequestMatcher xml(final String expectedXmlContent) {
@ -211,6 +209,7 @@ public class ContentRequestMatchers {
};
}
/**
* Abstract base class for XML {@link RequestMatcher}'s.
*/
@ -222,12 +221,13 @@ public class ContentRequestMatchers {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
matchInternal(mockRequest);
}
catch (Exception e) {
throw new AssertionError("Failed to parse expected or actual XML request content: " + e.getMessage());
catch (Exception ex) {
throw new AssertionError("Failed to parse expected or actual XML request content: " + ex.getMessage());
}
}
protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
}
}

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

Loading…
Cancel
Save