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
This commit is contained in:
Rossen Stoyanchev 2012-11-01 13:49:00 -04:00
parent 468f9c7814
commit 242bf7c4e3
22 changed files with 157 additions and 79 deletions

View File

@ -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")

View File

@ -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 <T> void assertValue(String content, Matcher<T> 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 {

View File

@ -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 <T> the static type accepted by the matcher
* @param actual the value to match against
* @param matcher the matcher
*/
public static <T> void assertThat(T actual, Matcher<T> matcher) {
assertThat("", actual, matcher);
}
/**
* Asserts that the given matcher matches the actual value.
*
* @param <T> 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 <T> void assertThat(String reason, T actual, Matcher<T> 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());
}
}
}

View File

@ -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<? super Node> 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<? super Source> matcher) throws Exception {
Document document = parseXmlString(content);
MatcherAssert.assertThat("Body content", new DOMSource(document), matcher);
assertThat("Body content", new DOMSource(document), matcher);
}
/**

View File

@ -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<? super Node> 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<? super String> 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<? super Double> 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);
}
/**

View File

@ -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));
}
};
}

View File

@ -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]);
}
}
};

View File

@ -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<? super String> 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));
}
};
}

View File

@ -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);
}
};
}

View File

@ -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);
}
};
}

View File

@ -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);
}
};
}

View File

@ -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<? super String> 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);
}
};
}

View File

@ -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);
}
};
}

View File

@ -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);
}
};
}

View File

@ -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<Integer> 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<? super String> 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);
}
};
}

View File

@ -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);
}
};
}

View File

@ -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);

View File

@ -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"))));
}

View File

@ -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<Integer> 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

View File

@ -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

View File

@ -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;
/**
*

View File

@ -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) {