diff --git a/spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java b/spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java index 0302686b2db..f07574e82e7 100644 --- a/spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java +++ b/spring-test/src/main/java/org/springframework/test/util/AssertionErrors.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. - * *
For example given: *
* assertEquals("Response header [" + name + "]", actual, expected);
@@ -51,7 +46,6 @@ public abstract class AssertionErrors {
*
* Response header [Accept] expected:<application/json> but was:<text/plain>
*
- *
* @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 {
*
* assertEquals("Response header [" + name + "]", actual, expected);
*
- *
* @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));
}
}
diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java
index 9dbea127ad7..e32a68aacc5 100644
--- a/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java
+++ b/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java
@@ -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 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.
- *
* Use of this matcher assumes the
* XMLUnit 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;
-
}
+
}
+
diff --git a/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java
index 08dc4c3323e..de034c24548 100644
--- a/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java
+++ b/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java
@@ -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("11 ".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 = "baz ";
this.request.getBody().write(content.getBytes());