From 242bf7c4e303ce6d51ba7c0a3943b0b815d685c8 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 1 Nov 2012 13:49:00 -0400 Subject: [PATCH] Refine hamcrest dependency in spring-test-mvc project 1) removed the hamcrest-all dependency requirement and replaced it with the more focused hamcrest-library dependency 2) added MatcherAssertionErrors as a replacement of org.hamcrest.MatcherAssert, which in hamcrest 1.1 is only available through the hamcrest-all dependency (and not in hamcrest-core nor in the hamcrest embedded in JUnit 4.4 through 4.8) 3) changed the required hamcrest version from 1.1 to 1.3 and made sure the spring-test-mvc project does not rely on newer hamcrest functionality without checking if it is available first Applications that already depend on older versions of hamcrest (in particular 1.1) via hamcrest-library, hamcrest-all or as part of junit 4.4 through 4.8 should not be disrupted if they add spring-test but may wish to exclude the hamcrest-library transitive dependency from spring-test in order to avoid extra jars in the classpath Applications that depend on hamcrest 1.3 should not have to do anything Issue: SPR-9940 --- build.gradle | 2 +- .../test/util/JsonPathExpectationsHelper.java | 4 +- .../test/util/MatcherAssertionErrors.java | 80 +++++++++++++++++++ .../test/util/XmlExpectationsHelper.java | 8 +- .../test/util/XpathExpectationsHelper.java | 10 +-- .../client/match/ContentRequestMatchers.java | 6 +- .../client/match/MockRestRequestMatchers.java | 8 +- .../servlet/result/ContentResultMatchers.java | 6 +- .../servlet/result/CookieResultMatchers.java | 14 ++-- .../result/FlashAttributeResultMatchers.java | 6 +- .../servlet/result/HandlerResultMatchers.java | 4 +- .../servlet/result/HeaderResultMatchers.java | 4 +- .../servlet/result/ModelResultMatchers.java | 4 +- .../servlet/result/RequestResultMatchers.java | 16 ++-- .../servlet/result/StatusResultMatchers.java | 6 +- .../servlet/result/ViewResultMatchers.java | 4 +- .../matchers/XpathRequestMatcherTests.java | 6 +- .../resultmatchers/ModelAssertionTests.java | 11 ++- .../resultmatchers/StatusAssertionTests.java | 7 +- .../resultmatchers/XpathAssertionTests.java | 10 ++- ...ConditionalDelegatingFilterProxyTests.java | 3 +- .../test/util/AssertionErrors.java | 17 ++-- 22 files changed, 157 insertions(+), 79 deletions(-) create mode 100644 spring-test-mvc/src/main/java/org/springframework/test/util/MatcherAssertionErrors.java diff --git a/build.gradle b/build.gradle index 162e29ab3d..97ef7ae57c 100644 --- a/build.gradle +++ b/build.gradle @@ -557,7 +557,7 @@ project('spring-test-mvc') { compile project(":spring-webmvc") compile project(":spring-test").sourceSets.main.output compile("org.apache.tomcat:tomcat-servlet-api:7.0.8", provided) - compile "org.hamcrest:hamcrest-all:1.1" + compile "org.hamcrest:hamcrest-library:1.3" compile("com.jayway.jsonpath:json-path:0.8.1", optional) compile("xmlunit:xmlunit:1.2", optional) testCompile("org.slf4j:jcl-over-slf4j:1.6.1") diff --git a/spring-test-mvc/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java b/spring-test-mvc/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java index 092b7685a0..6f42cb7241 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java @@ -16,13 +16,13 @@ package org.springframework.test.util; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import static org.springframework.test.util.AssertionErrors.assertTrue; import java.text.ParseException; import java.util.List; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import com.jayway.jsonpath.InvalidPathException; @@ -59,7 +59,7 @@ public class JsonPathExpectationsHelper { @SuppressWarnings("unchecked") public void assertValue(String content, Matcher matcher) throws ParseException { T value = (T) evaluateJsonPath(content); - MatcherAssert.assertThat("JSON path: " + this.expression, value, matcher); + assertThat("JSON path: " + this.expression, value, matcher); } private Object evaluateJsonPath(String content) throws ParseException { diff --git a/spring-test-mvc/src/main/java/org/springframework/test/util/MatcherAssertionErrors.java b/spring-test-mvc/src/main/java/org/springframework/test/util/MatcherAssertionErrors.java new file mode 100644 index 0000000000..6aa4cbdd7a --- /dev/null +++ b/spring-test-mvc/src/main/java/org/springframework/test/util/MatcherAssertionErrors.java @@ -0,0 +1,80 @@ +/* + * Copyright 2002-2012 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.test.util; + +import java.lang.reflect.Method; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.StringDescription; +import org.springframework.util.ClassUtils; + +/** + * A replacement of {@link org.hamcrest.MatcherAssert} that removes the need to + * depend on "hamcrest-all" when using Hamcrest 1.1 and also maintains backward + * compatibility with Hamcrest 1.1 (also embedded in JUnit 4.4 through 4.8). + * + * @author Rossen Stoyanchev + * @since 3.2 + */ +public abstract class MatcherAssertionErrors { + + private static final Method describeMismatchMethod = + ClassUtils.getMethodIfAvailable(Matcher.class, "describeMismatch", Object.class, Description.class); + + + private MatcherAssertionErrors() { + } + + /** + * Asserts that the given matcher matches the actual value. + * + * @param the static type accepted by the matcher + * @param actual the value to match against + * @param matcher the matcher + */ + public static void assertThat(T actual, Matcher matcher) { + assertThat("", actual, matcher); + } + + /** + * Asserts that the given matcher matches the actual value. + * + * @param the static type accepted by the matcher + * @param reason additional information about the error + * @param actual the value to match against + * @param matcher the matcher + */ + public static void assertThat(String reason, T actual, Matcher matcher) { + if (!matcher.matches(actual)) { + Description description = new StringDescription(); + description.appendText(reason); + description.appendText("\nExpected: "); + description.appendDescriptionOf(matcher); + if (describeMismatchMethod != null) { + description.appendText("\n but: "); + matcher.describeMismatch(actual, description); + } + else { + description.appendText("\n got: "); + description.appendValue(actual); + description.appendText("\n"); + } + throw new AssertionError(description.toString()); + } + } + +} diff --git a/spring-test-mvc/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java b/spring-test-mvc/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java index f9aed34217..cc575f0f9c 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java @@ -16,6 +16,8 @@ package org.springframework.test.util; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; + import java.io.StringReader; import java.util.Map; @@ -27,8 +29,6 @@ import javax.xml.transform.dom.DOMSource; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; -import org.springframework.test.util.AssertionErrors; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -49,7 +49,7 @@ public class XmlExpectationsHelper { */ public void assertNode(String content, Matcher matcher) throws Exception { Document document = parseXmlString(content); - MatcherAssert.assertThat("Body content", document, matcher); + assertThat("Body content", document, matcher); } private Document parseXmlString(String xml) throws Exception { @@ -67,7 +67,7 @@ public class XmlExpectationsHelper { */ public void assertSource(String content, Matcher matcher) throws Exception { Document document = parseXmlString(content); - MatcherAssert.assertThat("Body content", new DOMSource(document), matcher); + assertThat("Body content", new DOMSource(document), matcher); } /** diff --git a/spring-test-mvc/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java b/spring-test-mvc/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java index 1a52dde283..4aad26edda 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java @@ -17,6 +17,7 @@ package org.springframework.test.util; import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import java.io.StringReader; import java.util.Collections; @@ -32,7 +33,6 @@ import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.util.xml.SimpleNamespaceContext; import org.w3c.dom.Document; @@ -93,7 +93,7 @@ public class XpathExpectationsHelper { public void assertNode(String content, final Matcher matcher) throws Exception { Document document = parseXmlString(content); Node node = evaluateXpath(document, XPathConstants.NODE, Node.class); - MatcherAssert.assertThat("Xpath: " + XpathExpectationsHelper.this.expression, node, matcher); + assertThat("Xpath: " + XpathExpectationsHelper.this.expression, node, matcher); } /** @@ -149,7 +149,7 @@ public class XpathExpectationsHelper { Document document = parseXmlString(content); NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class); String reason = "nodeCount Xpath: " + XpathExpectationsHelper.this.expression; - MatcherAssert.assertThat(reason, nodeList.getLength(), matcher); + assertThat(reason, nodeList.getLength(), matcher); } /** @@ -169,7 +169,7 @@ public class XpathExpectationsHelper { public void assertString(String content, Matcher matcher) throws Exception { Document document = parseXmlString(content); String result = evaluateXpath(document, XPathConstants.STRING, String.class); - MatcherAssert.assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher); + assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher); } /** @@ -189,7 +189,7 @@ public class XpathExpectationsHelper { public void assertNumber(String content, Matcher matcher) throws Exception { Document document = parseXmlString(content); Double result = evaluateXpath(document, XPathConstants.NUMBER, Double.class); - MatcherAssert.assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher); + assertThat("Xpath: " + XpathExpectationsHelper.this.expression, result, matcher); } /** diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java index 220b006cef..d1e30db29f 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java @@ -16,6 +16,7 @@ package org.springframework.test.web.client.match; import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import static org.springframework.test.util.AssertionErrors.assertTrue; import java.io.IOException; @@ -24,7 +25,6 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpRequest; @@ -80,7 +80,7 @@ public class ContentRequestMatchers { return new RequestMatcher() { public void match(ClientHttpRequest request) throws IOException, AssertionError { MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; - MatcherAssert.assertThat("Request content", mockRequest.getBodyAsString(), matcher); + assertThat("Request content", mockRequest.getBodyAsString(), matcher); } }; } @@ -100,7 +100,7 @@ public class ContentRequestMatchers { public void match(ClientHttpRequest request) throws IOException, AssertionError { MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; byte[] content = mockRequest.getBodyAsBytes(); - MatcherAssert.assertThat("Request content", content, Matchers.equalTo(expectedContent)); + assertThat("Request content", content, Matchers.equalTo(expectedContent)); } }; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java index 5bb83fd844..5b388be475 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java @@ -15,6 +15,8 @@ */ package org.springframework.test.web.client.match; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; + import java.io.IOException; import java.net.URI; import java.util.List; @@ -23,13 +25,11 @@ import java.util.Map; import javax.xml.xpath.XPathExpressionException; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.hamcrest.core.IsEqual; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.client.ClientHttpRequest; -import org.springframework.mock.http.client.MockClientHttpRequest; import org.springframework.test.util.AssertionErrors; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.test.web.client.RequestMatcher; @@ -75,7 +75,7 @@ public abstract class MockRestRequestMatchers { Assert.notNull(matcher, "'matcher' must not be null"); return new RequestMatcher() { public void match(ClientHttpRequest request) throws IOException, AssertionError { - MatcherAssert.assertThat("Request URI", request.getURI().toString(), matcher); + assertThat("Request URI", request.getURI().toString(), matcher); } }; } @@ -133,7 +133,7 @@ public abstract class MockRestRequestMatchers { AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + matchers.length + "> values but it has only <" + values.size() + ">", matchers.length <= values.size()); for (int i = 0 ; i < matchers.length; i++) { - MatcherAssert.assertThat("Request header", headers.get(name).get(i), matchers[i]); + assertThat("Request header", headers.get(name).get(i), matchers[i]); } } }; diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java index d76284d800..d196582dae 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java @@ -17,6 +17,7 @@ package org.springframework.test.web.servlet.result; import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import static org.springframework.test.util.AssertionErrors.assertTrue; import java.util.Map; @@ -26,7 +27,6 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.http.MediaType; import org.springframework.test.util.XmlExpectationsHelper; @@ -97,7 +97,7 @@ public class ContentResultMatchers { public ResultMatcher string(final Matcher matcher) { return new ResultMatcher() { public void match(MvcResult result) throws Exception { - MatcherAssert.assertThat("Response content", result.getResponse().getContentAsString(), matcher); + assertThat("Response content", result.getResponse().getContentAsString(), matcher); } }; } @@ -116,7 +116,7 @@ public class ContentResultMatchers { return new ResultMatcher() { public void match(MvcResult result) throws Exception { byte[] content = result.getResponse().getContentAsByteArray(); - MatcherAssert.assertThat("Response content", content, Matchers.equalTo(expectedContent)); + assertThat("Response content", content, Matchers.equalTo(expectedContent)); } }; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java index 5d00077acf..4cdaeb0637 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java @@ -17,12 +17,12 @@ package org.springframework.test.web.servlet.result; import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import static org.springframework.test.util.AssertionErrors.assertTrue; import javax.servlet.http.Cookie; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; @@ -53,7 +53,7 @@ public class CookieResultMatchers { public void match(MvcResult result) { Cookie cookie = result.getResponse().getCookie(name); assertTrue("Response cookie not found: " + name, cookie != null); - MatcherAssert.assertThat("Response cookie", cookie.getValue(), matcher); + assertThat("Response cookie", cookie.getValue(), matcher); } }; } @@ -99,7 +99,7 @@ public class CookieResultMatchers { public void match(MvcResult result) { Cookie cookie = result.getResponse().getCookie(name); assertTrue("No cookie with name: " + name, cookie != null); - MatcherAssert.assertThat("Response cookie maxAge", cookie.getMaxAge(), matcher); + assertThat("Response cookie maxAge", cookie.getMaxAge(), matcher); } }; } @@ -118,7 +118,7 @@ public class CookieResultMatchers { return new ResultMatcher() { public void match(MvcResult result) throws Exception { Cookie cookie = result.getResponse().getCookie(name); - MatcherAssert.assertThat("Response cookie path", cookie.getPath(), matcher); + assertThat("Response cookie path", cookie.getPath(), matcher); } }; } @@ -134,7 +134,7 @@ public class CookieResultMatchers { return new ResultMatcher() { public void match(MvcResult result) throws Exception { Cookie cookie = result.getResponse().getCookie(name); - MatcherAssert.assertThat("Response cookie domain", cookie.getDomain(), matcher); + assertThat("Response cookie domain", cookie.getDomain(), matcher); } }; } @@ -153,7 +153,7 @@ public class CookieResultMatchers { return new ResultMatcher() { public void match(MvcResult result) throws Exception { Cookie cookie = result.getResponse().getCookie(name); - MatcherAssert.assertThat("Response cookie comment", cookie.getComment(), matcher); + assertThat("Response cookie comment", cookie.getComment(), matcher); } }; } @@ -172,7 +172,7 @@ public class CookieResultMatchers { return new ResultMatcher() { public void match(MvcResult result) throws Exception { Cookie cookie = result.getResponse().getCookie(name); - MatcherAssert.assertThat("Response cookie version", cookie.getVersion(), matcher); + assertThat("Response cookie version", cookie.getVersion(), matcher); } }; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java index 191be8485f..cf129a852d 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java @@ -16,10 +16,10 @@ package org.springframework.test.web.servlet.result; -import static org.springframework.test.util.AssertionErrors.*; +import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; @@ -48,7 +48,7 @@ public class FlashAttributeResultMatchers { return new ResultMatcher() { @SuppressWarnings("unchecked") public void match(MvcResult result) throws Exception { - MatcherAssert.assertThat("Flash attribute", (T) result.getFlashMap().get(name), matcher); + assertThat("Flash attribute", (T) result.getFlashMap().get(name), matcher); } }; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java index a01488dcae..6932bf6fd0 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java @@ -17,12 +17,12 @@ package org.springframework.test.web.servlet.result; import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import static org.springframework.test.util.AssertionErrors.assertTrue; import java.lang.reflect.Method; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; @@ -78,7 +78,7 @@ public class HandlerResultMatchers { Object handler = result.getHandler(); assertTrue("No handler: ", handler != null); assertTrue("Not a HandlerMethod: " + handler, HandlerMethod.class.isInstance(handler)); - MatcherAssert.assertThat("HandlerMethod", ((HandlerMethod) handler).getMethod().getName(), matcher); + assertThat("HandlerMethod", ((HandlerMethod) handler).getMethod().getName(), matcher); } }; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java index 51e2e3a044..4720952115 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java @@ -17,9 +17,9 @@ package org.springframework.test.web.servlet.result; import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; @@ -47,7 +47,7 @@ public class HeaderResultMatchers { public ResultMatcher string(final String name, final Matcher matcher) { return new ResultMatcher() { public void match(MvcResult result) { - MatcherAssert.assertThat("Response header", result.getResponse().getHeader(name), matcher); + assertThat("Response header", result.getResponse().getHeader(name), matcher); } }; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java index 3d79b7e7c2..bff0294bfe 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java @@ -17,10 +17,10 @@ package org.springframework.test.web.servlet.result; import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import static org.springframework.test.util.AssertionErrors.assertTrue; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; @@ -54,7 +54,7 @@ public class ModelResultMatchers { @SuppressWarnings("unchecked") public void match(MvcResult result) throws Exception { ModelAndView mav = getModelAndView(result); - MatcherAssert.assertThat("Model attribute '" + name + "'", (T) mav.getModel().get(name), matcher); + assertThat("Model attribute '" + name + "'", (T) mav.getModel().get(name), matcher); } }; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java index 3b6d1cf15e..d27f23f060 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/RequestResultMatchers.java @@ -17,19 +17,19 @@ package org.springframework.test.web.servlet.result; import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import java.util.concurrent.Callable; import javax.servlet.http.HttpServletRequest; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; -import org.springframework.web.context.request.async.MvcAsyncTask; import org.springframework.web.context.request.async.DeferredResult; +import org.springframework.web.context.request.async.MvcAsyncTask; /** * Factory for assertions on the request. An instance of this class is @@ -62,7 +62,7 @@ public class RequestResultMatchers { return new ResultMatcher() { public void match(MvcResult result) { HttpServletRequest request = result.getRequest(); - MatcherAssert.assertThat("Async started", request.isAsyncStarted(), equalTo(true)); + assertThat("Async started", request.isAsyncStarted(), equalTo(true)); } }; } @@ -75,7 +75,7 @@ public class RequestResultMatchers { return new ResultMatcher() { public void match(MvcResult result) { HttpServletRequest request = result.getRequest(); - MatcherAssert.assertThat("Async started", request.isAsyncStarted(), equalTo(false)); + assertThat("Async started", request.isAsyncStarted(), equalTo(false)); } }; } @@ -88,8 +88,8 @@ public class RequestResultMatchers { @SuppressWarnings("unchecked") public void match(MvcResult result) { HttpServletRequest request = result.getRequest(); - MatcherAssert.assertThat("Async started", request.isAsyncStarted(), equalTo(true)); - MatcherAssert.assertThat("Async result", (T) result.getAsyncResult(), matcher); + assertThat("Async started", request.isAsyncStarted(), equalTo(true)); + assertThat("Async result", (T) result.getAsyncResult(), matcher); } }; } @@ -112,7 +112,7 @@ public class RequestResultMatchers { @SuppressWarnings("unchecked") public void match(MvcResult result) { T value = (T) result.getRequest().getAttribute(name); - MatcherAssert.assertThat("Request attribute: ", value, matcher); + assertThat("Request attribute: ", value, matcher); } }; } @@ -132,7 +132,7 @@ public class RequestResultMatchers { @SuppressWarnings("unchecked") public void match(MvcResult result) { T value = (T) result.getRequest().getSession().getAttribute(name); - MatcherAssert.assertThat("Request attribute: ", value, matcher); + assertThat("Request attribute: ", value, matcher); } }; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java index 370c838b11..29ce3ed245 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/StatusResultMatchers.java @@ -16,9 +16,9 @@ package org.springframework.test.web.servlet.result; import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.http.HttpStatus; import org.springframework.test.web.servlet.MvcResult; @@ -48,7 +48,7 @@ public class StatusResultMatchers { public ResultMatcher is(final Matcher matcher) { return new ResultMatcher() { public void match(MvcResult result) throws Exception { - MatcherAssert.assertThat("Status: ", result.getResponse().getStatus(), matcher); + assertThat("Status: ", result.getResponse().getStatus(), matcher); } }; } @@ -67,7 +67,7 @@ public class StatusResultMatchers { public ResultMatcher reason(final Matcher matcher) { return new ResultMatcher() { public void match(MvcResult result) throws Exception { - MatcherAssert.assertThat("Status reason: ", result.getResponse().getErrorMessage(), matcher); + assertThat("Status reason: ", result.getResponse().getErrorMessage(), matcher); } }; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ViewResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ViewResultMatchers.java index 454ea4d03d..89dc707831 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ViewResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ViewResultMatchers.java @@ -16,10 +16,10 @@ package org.springframework.test.web.servlet.result; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import static org.springframework.test.util.AssertionErrors.assertTrue; import org.hamcrest.Matcher; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; @@ -48,7 +48,7 @@ public class ViewResultMatchers { public void match(MvcResult result) throws Exception { ModelAndView mav = result.getModelAndView(); assertTrue("No ModelAndView found", mav != null); - MatcherAssert.assertThat("View name", mav.getViewName(), matcher); + assertThat("View name", mav.getViewName(), matcher); } }; } diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/client/samples/matchers/XpathRequestMatcherTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/client/samples/matchers/XpathRequestMatcherTests.java index b26f3cd29d..3ffd7485f8 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/client/samples/matchers/XpathRequestMatcherTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/client/samples/matchers/XpathRequestMatcherTests.java @@ -17,8 +17,6 @@ package org.springframework.test.web.client.samples.matchers; import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.startsWith; import static org.springframework.test.web.client.match.MockRestRequestMatchers.content; @@ -192,8 +190,8 @@ public class XpathRequestMatcherTests { .andExpect(content().contentType("application/xml")) .andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(4)) .andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(2)) - .andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(lessThan(5))) // Hamcrest.. - .andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(greaterThan(0))) // Hamcrest.. + .andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(equalTo(4))) // Hamcrest.. + .andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(equalTo(2))) // Hamcrest.. .andRespond(withSuccess()); this.restTemplate.put(new URI("/composers"), this.people); diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ModelAssertionTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ModelAssertionTests.java index a7f76e1813..b34d2577ee 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ModelAssertionTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ModelAssertionTests.java @@ -19,14 +19,14 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.startsWith; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; import javax.validation.Valid; @@ -78,11 +78,10 @@ public class ModelAssertionTests { .andExpect(model().attribute("INTEGER", nullValue())); } - @SuppressWarnings("unchecked") @Test public void testAttributeHamcrestMatchers() throws Exception { mockMvc.perform(get("/")) - .andExpect(model().attribute("integer", allOf(greaterThan(2), lessThan(4)))) + .andExpect(model().attribute("integer", equalTo(3))) .andExpect(model().attribute("string", allOf(startsWith("a string"), endsWith("value")))) .andExpect(model().attribute("person", hasProperty("name", equalTo("a name")))); } diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/StatusAssertionTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/StatusAssertionTests.java index 96b456b187..517036febf 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/StatusAssertionTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/StatusAssertionTests.java @@ -16,16 +16,12 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers; -import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.hamcrest.Matchers.lessThan; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; -import org.hamcrest.Matcher; import org.junit.Before; import org.junit.Test; import org.springframework.http.HttpStatus; @@ -63,8 +59,7 @@ public class StatusAssertionTests { @Test public void testMatcher() throws Exception { - Matcher matcher = allOf(greaterThanOrEqualTo(400), lessThan(500)); - this.mockMvc.perform(get("/badRequest")).andExpect(status().is(matcher)); + this.mockMvc.perform(get("/badRequest")).andExpect(status().is(equalTo(400))); } @Test diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/XpathAssertionTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/XpathAssertionTests.java index 137e3e2d50..7c941d652b 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/XpathAssertionTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/XpathAssertionTests.java @@ -16,7 +16,11 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.closeTo; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.startsWith; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -145,8 +149,8 @@ public class XpathAssertionTests { this.mockMvc.perform(get("/music/people")) .andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(4)) .andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(2)) - .andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(lessThan(5))) // Hamcrest.. - .andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(greaterThan(0))); + .andExpect(xpath("/ns:people/composers/composer", NS).nodeCount(equalTo(4))) // Hamcrest.. + .andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(equalTo(2))); } @Controller diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/setup/ConditionalDelegatingFilterProxyTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/setup/ConditionalDelegatingFilterProxyTests.java index 75484889e4..9282d4885a 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/setup/ConditionalDelegatingFilterProxyTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/setup/ConditionalDelegatingFilterProxyTests.java @@ -12,9 +12,9 @@ */ package org.springframework.test.web.servlet.setup; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.springframework.test.util.MatcherAssertionErrors.assertThat; import java.io.IOException; @@ -31,7 +31,6 @@ import org.springframework.mock.web.MockFilterChain; import org.springframework.mock.web.MockFilterConfig; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.test.web.servlet.setup.PatternMappingFilterProxy; /** * 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 ab112e698a..bc31c97582 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 @@ -37,20 +37,22 @@ public abstract class AssertionErrors { } /** - * Fails a test with the given message passing along expected and actual values to be added to the message. + * Fails a test with the given message passing along expected and actual + * values to be added to the message. * * @param message the message * @param expected the expected value - * @param actual the actual value + * @param actual the actual value */ public static void fail(String message, Object expected, Object actual) { throw new AssertionError(message + " expected:<" + expected + "> but was:<" + actual + ">"); } /** - * Asserts that a condition is {@code true}. If not, throws an {@link AssertionError} with the given message. + * Asserts that a condition is {@code true}. If not, throws an + * {@link AssertionError} with the given message. * - * @param message the message + * @param message the message * @param condition the condition to test for */ public static void assertTrue(String message, boolean condition) { @@ -60,11 +62,12 @@ public abstract class AssertionErrors { } /** - * Asserts that two objects are equal. If not, an {@link AssertionError} is thrown with the given message. + * Asserts that two objects are equal. If not, an {@link AssertionError} is + * thrown with the given message. * - * @param message the message + * @param message the message * @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) { if (expected == null && actual == null) {