An instance of this class is typically accessed via
* {@link MockMvcResultMatchers#content}.
*
@@ -74,13 +74,10 @@ public class ContentResultMatchers {
* {@link #contentTypeCompatibleWith(MediaType)}.
*/
public ResultMatcher contentType(final MediaType contentType) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- String actual = result.getResponse().getContentType();
- assertTrue("Content type not set", actual != null);
- assertEquals("Content type", contentType, MediaType.parseMediaType(actual));
- }
+ return result -> {
+ String actual = result.getResponse().getContentType();
+ assertTrue("Content type not set", actual != null);
+ assertEquals("Content type", contentType, MediaType.parseMediaType(actual));
};
}
@@ -97,15 +94,12 @@ public class ContentResultMatchers {
* content type as defined by {@link MediaType#isCompatibleWith(MediaType)}.
*/
public ResultMatcher contentTypeCompatibleWith(final MediaType contentType) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- String actual = result.getResponse().getContentType();
- assertTrue("Content type not set", actual != null);
- MediaType actualContentType = MediaType.parseMediaType(actual);
- assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]",
- actualContentType.isCompatibleWith(contentType));
- }
+ return result -> {
+ String actual = result.getResponse().getContentType();
+ assertTrue("Content type not set", actual != null);
+ MediaType actualContentType = MediaType.parseMediaType(actual);
+ assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]",
+ actualContentType.isCompatibleWith(contentType));
};
}
@@ -114,12 +108,9 @@ public class ContentResultMatchers {
* @see HttpServletResponse#getCharacterEncoding()
*/
public ResultMatcher encoding(final String characterEncoding) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- String actual = result.getResponse().getCharacterEncoding();
- assertEquals("Character encoding", characterEncoding, actual);
- }
+ return result -> {
+ String actual = result.getResponse().getCharacterEncoding();
+ assertEquals("Character encoding", characterEncoding, actual);
};
}
@@ -131,36 +122,21 @@ public class ContentResultMatchers {
*
*/
public ResultMatcher string(final Matcher super String> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- assertThat("Response content", result.getResponse().getContentAsString(), matcher);
- }
- };
+ return result -> assertThat("Response content", result.getResponse().getContentAsString(), matcher);
}
/**
* Assert the response body content as a String.
*/
public ResultMatcher string(final String expectedContent) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- assertEquals("Response content", expectedContent, result.getResponse().getContentAsString());
- }
- };
+ return result -> assertEquals("Response content", expectedContent, result.getResponse().getContentAsString());
}
/**
* Assert the response body content as a byte array.
*/
public ResultMatcher bytes(final byte[] expectedContent) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- assertEquals("Response content", expectedContent, result.getResponse().getContentAsByteArray());
- }
- };
+ return result -> assertEquals("Response content", expectedContent, result.getResponse().getContentAsByteArray());
}
/**
@@ -174,12 +150,9 @@ public class ContentResultMatchers {
* @see MockMvcResultMatchers#xpath(String, Map, Object...)
*/
public ResultMatcher xml(final String xmlContent) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- String content = result.getResponse().getContentAsString();
- xmlHelper.assertXmlEqual(xmlContent, content);
- }
+ return result -> {
+ String content = result.getResponse().getContentAsString();
+ xmlHelper.assertXmlEqual(xmlContent, content);
};
}
@@ -188,12 +161,9 @@ public class ContentResultMatchers {
* {@link Matcher}.
*/
public ResultMatcher node(final Matcher super Node> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- String content = result.getResponse().getContentAsString();
- xmlHelper.assertNode(content, matcher);
- }
+ return result -> {
+ String content = result.getResponse().getContentAsString();
+ xmlHelper.assertNode(content, matcher);
};
}
@@ -203,12 +173,9 @@ public class ContentResultMatchers {
* @see xml-matchers
*/
public ResultMatcher source(final Matcher super Source> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- String content = result.getResponse().getContentAsString();
- xmlHelper.assertSource(content, matcher);
- }
+ return result -> {
+ String content = result.getResponse().getContentAsString();
+ xmlHelper.assertSource(content, matcher);
};
}
@@ -217,7 +184,6 @@ public class ContentResultMatchers {
* are "similar" - i.e. they contain the same attribute-value pairs
* regardless of formatting with a lenient checking (extensible, and non-strict array
* ordering).
- *
* @param jsonContent the expected JSON content
* @since 4.1
*/
@@ -226,30 +192,23 @@ public class ContentResultMatchers {
}
/**
- * Parse the response content and the given string as JSON and assert the two
- * are "similar" - i.e. they contain the same attribute-value pairs
- * regardless of formatting.
- *
+ * Parse the response content and the given string as JSON and assert the two are "similar" -
+ * i.e. they contain the same attribute-value pairs regardless of formatting.
*
Can compare in two modes, depending on {@code strict} parameter value:
*
- *
{@code true}: strict checking. Not extensible, and strict array ordering.
- *
{@code false}: lenient checking. Extensible, and non-strict array ordering.
+ *
{@code true}: strict checking. Not extensible, and strict array ordering.
+ *
{@code false}: lenient checking. Extensible, and non-strict array ordering.
*
- *
*
Use of this matcher requires the JSONassert library.
- *
* @param jsonContent the expected JSON content
* @param strict enables strict checking
* @since 4.2
*/
public ResultMatcher json(final String jsonContent, final boolean strict) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- String content = result.getResponse().getContentAsString();
- jsonHelper.assertJsonEqual(jsonContent, content, strict);
- }
+ return result -> {
+ String content = result.getResponse().getContentAsString();
+ jsonHelper.assertJsonEqual(jsonContent, content, strict);
};
}
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java
index caa5ad14653..45ef7c90149 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2017 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.
@@ -20,7 +20,6 @@ import javax.servlet.http.Cookie;
import org.hamcrest.Matcher;
-import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import static org.hamcrest.MatcherAssert.*;
@@ -28,6 +27,7 @@ import static org.springframework.test.util.AssertionErrors.*;
/**
* Factory for response cookie assertions.
+ *
*
An instance of this class is typically accessed via
* {@link MockMvcResultMatchers#cookie}.
*
@@ -49,13 +49,10 @@ public class CookieResultMatchers {
* Assert a cookie value with the given Hamcrest {@link Matcher}.
*/
public ResultMatcher value(final String name, final Matcher super String> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- Cookie cookie = result.getResponse().getCookie(name);
- assertTrue("Response cookie not found: " + name, cookie != null);
- assertThat("Response cookie", cookie.getValue(), matcher);
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertTrue("Response cookie '" + name + "' not found", cookie != null);
+ assertThat("Response cookie '" + name + "'", cookie.getValue(), matcher);
};
}
@@ -63,13 +60,10 @@ public class CookieResultMatchers {
* Assert a cookie value.
*/
public ResultMatcher value(final String name, final String expectedValue) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- Cookie cookie = result.getResponse().getCookie(name);
- assertTrue("Response cookie not found: " + name, cookie != null);
- assertEquals("Response cookie", expectedValue, cookie.getValue());
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertTrue("Response cookie '" + name + "' not found", cookie != null);
+ assertEquals("Response cookie", expectedValue, cookie.getValue());
};
}
@@ -78,12 +72,9 @@ public class CookieResultMatchers {
* max age is 0 (i.e. expired).
*/
public ResultMatcher exists(final String name) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- Cookie cookie = result.getResponse().getCookie(name);
- assertTrue("No cookie with name: " + name, cookie != null);
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertTrue("No cookie with name '" + name + "'", cookie != null);
};
}
@@ -92,12 +83,9 @@ public class CookieResultMatchers {
* irrespective of whether max age is 0, i.e. expired.
*/
public ResultMatcher doesNotExist(final String name) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- Cookie cookie = result.getResponse().getCookie(name);
- assertTrue("Unexpected cookie with name " + name, cookie == null);
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertTrue("Unexpected cookie with name '" + name + "'", cookie == null);
};
}
@@ -105,13 +93,10 @@ public class CookieResultMatchers {
* Assert a cookie's maxAge with a Hamcrest {@link Matcher}.
*/
public ResultMatcher maxAge(final String name, final Matcher super Integer> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- Cookie cookie = result.getResponse().getCookie(name);
- assertTrue("No cookie with name: " + name, cookie != null);
- assertThat("Response cookie maxAge", cookie.getMaxAge(), matcher);
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertTrue("No cookie with name '" + name + "'", cookie != null);
+ assertThat("Response cookie '" + name + "' maxAge", cookie.getMaxAge(), matcher);
};
}
@@ -119,13 +104,10 @@ public class CookieResultMatchers {
* Assert a cookie's maxAge value.
*/
public ResultMatcher maxAge(final String name, final int maxAge) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- Cookie cookie = result.getResponse().getCookie(name);
- assertTrue("No cookie with name: " + name, cookie != null);
- assertEquals("Response cookie maxAge", maxAge, cookie.getMaxAge());
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertTrue("No cookie with name: " + name, cookie != null);
+ assertEquals("Response cookie '" + name + "' maxAge", maxAge, cookie.getMaxAge());
};
}
@@ -133,22 +115,16 @@ public class CookieResultMatchers {
* Assert a cookie path with a Hamcrest {@link Matcher}.
*/
public ResultMatcher path(final String name, final Matcher super String> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Cookie cookie = result.getResponse().getCookie(name);
- assertThat("Response cookie path", cookie.getPath(), matcher);
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertThat("Response cookie '" + name + "' path", cookie.getPath(), matcher);
};
}
public ResultMatcher path(final String name, final String path) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Cookie cookie = result.getResponse().getCookie(name);
- assertEquals("Response cookie path", path, cookie.getPath());
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertEquals("Response cookie '" + name + "' path", path, cookie.getPath());
};
}
@@ -156,12 +132,9 @@ public class CookieResultMatchers {
* Assert a cookie's domain with a Hamcrest {@link Matcher}.
*/
public ResultMatcher domain(final String name, final Matcher super String> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Cookie cookie = result.getResponse().getCookie(name);
- assertThat("Response cookie domain", cookie.getDomain(), matcher);
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertThat("Response cookie '" + name + "' domain", cookie.getDomain(), matcher);
};
}
@@ -169,12 +142,9 @@ public class CookieResultMatchers {
* Assert a cookie's domain value.
*/
public ResultMatcher domain(final String name, final String domain) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Cookie cookie = result.getResponse().getCookie(name);
- assertEquals("Response cookie domain", domain, cookie.getDomain());
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertEquals("Response cookie '" + name + "' domain", domain, cookie.getDomain());
};
}
@@ -182,12 +152,9 @@ public class CookieResultMatchers {
* Assert a cookie's comment with a Hamcrest {@link Matcher}.
*/
public ResultMatcher comment(final String name, final Matcher super String> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Cookie cookie = result.getResponse().getCookie(name);
- assertThat("Response cookie comment", cookie.getComment(), matcher);
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertThat("Response cookie '" + name + "' comment", cookie.getComment(), matcher);
};
}
@@ -195,12 +162,9 @@ public class CookieResultMatchers {
* Assert a cookie's comment value.
*/
public ResultMatcher comment(final String name, final String comment) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Cookie cookie = result.getResponse().getCookie(name);
- assertEquals("Response cookie comment", comment, cookie.getComment());
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertEquals("Response cookie '" + name + "' comment", comment, cookie.getComment());
};
}
@@ -208,12 +172,9 @@ public class CookieResultMatchers {
* Assert a cookie's version with a Hamcrest {@link Matcher}
*/
public ResultMatcher version(final String name, final Matcher super Integer> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Cookie cookie = result.getResponse().getCookie(name);
- assertThat("Response cookie version", cookie.getVersion(), matcher);
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertThat("Response cookie '" + name + "' version", cookie.getVersion(), matcher);
};
}
@@ -221,12 +182,9 @@ public class CookieResultMatchers {
* Assert a cookie's version value.
*/
public ResultMatcher version(final String name, final int version) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Cookie cookie = result.getResponse().getCookie(name);
- assertEquals("Response cookie version", version, cookie.getVersion());
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertEquals("Response cookie '" + name + "' version", version, cookie.getVersion());
};
}
@@ -234,12 +192,9 @@ public class CookieResultMatchers {
* Assert whether the cookie must be sent over a secure protocol or not.
*/
public ResultMatcher secure(final String name, final boolean secure) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Cookie cookie = result.getResponse().getCookie(name);
- assertEquals("Response cookie secure", secure, cookie.getSecure());
- }
+ return result -> {
+ Cookie cookie = result.getResponse().getCookie(name);
+ assertEquals("Response cookie '" + name + "' secure", secure, cookie.getSecure());
};
}
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java
index 78bf09d05a5..40410e6b872 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2017 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.
@@ -18,7 +18,6 @@ package org.springframework.test.web.servlet.result;
import org.hamcrest.Matcher;
-import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import static org.hamcrest.MatcherAssert.*;
@@ -26,6 +25,7 @@ import static org.springframework.test.util.AssertionErrors.*;
/**
* Factory for "output" flash attribute assertions.
+ *
*
An instance of this class is typically accessed via
* {@link MockMvcResultMatchers#flash}.
*
@@ -45,38 +45,25 @@ public class FlashAttributeResultMatchers {
/**
* Assert a flash attribute's value with the given Hamcrest {@link Matcher}.
*/
+ @SuppressWarnings("unchecked")
public ResultMatcher attribute(final String name, final Matcher matcher) {
- return new ResultMatcher() {
- @Override
- @SuppressWarnings("unchecked")
- public void match(MvcResult result) throws Exception {
- assertThat("Flash attribute", (T) result.getFlashMap().get(name), matcher);
- }
- };
+ return result -> assertThat("Flash attribute '" + name + "'", (T) result.getFlashMap().get(name), matcher);
}
/**
* Assert a flash attribute's value.
*/
public ResultMatcher attribute(final String name, final Object value) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- assertEquals("Flash attribute", value, result.getFlashMap().get(name));
- }
- };
+ return result -> assertEquals("Flash attribute '" + name + "'", value, result.getFlashMap().get(name));
}
/**
* Assert the existence of the given flash attributes.
*/
public ResultMatcher attributeExists(final String... names) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- for (String name : names) {
- assertTrue("Flash attribute [" + name + "] does not exist", result.getFlashMap().get(name) != null);
- }
+ return result -> {
+ for (String name : names) {
+ assertTrue("Flash attribute '" + name + "' does not exist", result.getFlashMap().get(name) != null);
}
};
}
@@ -85,12 +72,7 @@ public class FlashAttributeResultMatchers {
* Assert the number of flash attributes.
*/
public ResultMatcher attributeCount(final int count) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- assertEquals("FlashMap size", count, result.getFlashMap().size());
- }
- };
+ return result -> assertEquals("FlashMap size must be " + count, count, result.getFlashMap().size());
}
}
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java
index 211ca9a7831..15f366ebc25 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2016 the original author or authors.
+ * Copyright 2002-2017 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.
@@ -29,13 +29,12 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.springframework.test.util.AssertionErrors.assertEquals;
-import static org.springframework.test.util.AssertionErrors.assertTrue;
-import static org.springframework.test.util.AssertionErrors.fail;
+import static org.hamcrest.MatcherAssert.*;
+import static org.springframework.test.util.AssertionErrors.*;
/**
* Factory for assertions on the selected handler or handler method.
+ *
*
An instance of this class is typically accessed via
* {@link MockMvcResultMatchers#handler}.
*
@@ -50,7 +49,6 @@ import static org.springframework.test.util.AssertionErrors.fail;
*/
public class HandlerResultMatchers {
-
/**
* Protected constructor.
* Use {@link MockMvcResultMatchers#handler()}.
@@ -63,17 +61,14 @@ public class HandlerResultMatchers {
* Assert the type of the handler that processed the request.
*/
public ResultMatcher handlerType(final Class> type) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- Object handler = result.getHandler();
- assertTrue("No handler: ", handler != null);
- Class> actual = handler.getClass();
- if (HandlerMethod.class.isInstance(handler)) {
- actual = ((HandlerMethod) handler).getBeanType();
- }
- assertEquals("Handler type", type, ClassUtils.getUserClass(actual));
+ return result -> {
+ Object handler = result.getHandler();
+ assertTrue("No handler", handler != null);
+ Class> actual = handler.getClass();
+ if (HandlerMethod.class.isInstance(handler)) {
+ actual = ((HandlerMethod) handler).getBeanType();
}
+ assertEquals("Handler type", type, ClassUtils.getUserClass(actual));
};
}
@@ -103,19 +98,16 @@ public class HandlerResultMatchers {
* or the "mock" controller itself after an invocation
*/
public ResultMatcher methodCall(final Object obj) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- if (!MethodInvocationInfo.class.isInstance(obj)) {
- fail(String.format("The supplied object [%s] is not an instance of %s. "
- + "Ensure that you invoke the handler method via MvcUriComponentsBuilder.on().",
- obj, MethodInvocationInfo.class.getName()));
- }
- MethodInvocationInfo invocationInfo = (MethodInvocationInfo) obj;
- Method expected = invocationInfo.getControllerMethod();
- Method actual = getHandlerMethod(result).getMethod();
- assertEquals("Handler method", expected, actual);
+ return result -> {
+ if (!(obj instanceof MethodInvocationInfo)) {
+ fail(String.format("The supplied object [%s] is not an instance of %s. " +
+ "Ensure that you invoke the handler method via MvcUriComponentsBuilder.on().",
+ obj, MethodInvocationInfo.class.getName()));
}
+ MethodInvocationInfo invocationInfo = (MethodInvocationInfo) obj;
+ Method expected = invocationInfo.getControllerMethod();
+ Method actual = getHandlerMethod(result).getMethod();
+ assertEquals("Handler method", expected, actual);
};
}
@@ -124,12 +116,9 @@ public class HandlerResultMatchers {
* using the given Hamcrest {@link Matcher}.
*/
public ResultMatcher methodName(final Matcher super String> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- HandlerMethod handlerMethod = getHandlerMethod(result);
- assertThat("Handler method", handlerMethod.getMethod().getName(), matcher);
- }
+ return result -> {
+ HandlerMethod handlerMethod = getHandlerMethod(result);
+ assertThat("Handler method", handlerMethod.getMethod().getName(), matcher);
};
}
@@ -137,12 +126,9 @@ public class HandlerResultMatchers {
* Assert the name of the controller method used to process the request.
*/
public ResultMatcher methodName(final String name) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- HandlerMethod handlerMethod = getHandlerMethod(result);
- assertEquals("Handler method", name, handlerMethod.getMethod().getName());
- }
+ return result -> {
+ HandlerMethod handlerMethod = getHandlerMethod(result);
+ assertEquals("Handler method", name, handlerMethod.getMethod().getName());
};
}
@@ -150,19 +136,17 @@ public class HandlerResultMatchers {
* Assert the controller method used to process the request.
*/
public ResultMatcher method(final Method method) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) throws Exception {
- HandlerMethod handlerMethod = getHandlerMethod(result);
- assertEquals("Handler method", method, handlerMethod.getMethod());
- }
+ return result -> {
+ HandlerMethod handlerMethod = getHandlerMethod(result);
+ assertEquals("Handler method", method, handlerMethod.getMethod());
};
}
+
private static HandlerMethod getHandlerMethod(MvcResult result) {
Object handler = result.getHandler();
- assertTrue("No handler: ", handler != null);
- assertTrue("Not a HandlerMethod: " + handler, HandlerMethod.class.isInstance(handler));
+ assertTrue("No handler", handler != null);
+ assertTrue("Not a HandlerMethod: " + handler, handler instanceof HandlerMethod);
return (HandlerMethod) handler;
}
diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
index aac0bab91a5..02ef34a9f3d 100644
--- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
+++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2016 the original author or authors.
+ * Copyright 2002-2017 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.
@@ -26,15 +26,14 @@ import java.util.TimeZone;
import org.hamcrest.Matcher;
import org.springframework.mock.web.MockHttpServletResponse;
-import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.springframework.test.util.AssertionErrors.assertEquals;
-import static org.springframework.test.util.AssertionErrors.assertTrue;
+import static org.hamcrest.MatcherAssert.*;
+import static org.springframework.test.util.AssertionErrors.*;
/**
* Factory for response header assertions.
+ *
*
An instance of this class is available via
* {@link MockMvcResultMatchers#header}.
*
@@ -45,7 +44,6 @@ import static org.springframework.test.util.AssertionErrors.assertTrue;
*/
public class HeaderResultMatchers {
-
/**
* Protected constructor.
* See {@link MockMvcResultMatchers#header()}.
@@ -59,12 +57,7 @@ public class HeaderResultMatchers {
* String {@code Matcher}.
*/
public ResultMatcher string(final String name, final Matcher super String> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- assertThat("Response header " + name, result.getResponse().getHeader(name), matcher);
- }
- };
+ return result -> assertThat("Response header '" + name + "'", result.getResponse().getHeader(name), matcher);
}
/**
@@ -73,12 +66,9 @@ public class HeaderResultMatchers {
* @since 4.3
*/
public ResultMatcher stringValues(final String name, final Matcher> matcher) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- List values = result.getResponse().getHeaders(name);
- assertThat("Response header " + name, values, matcher);
- }
+ return result -> {
+ List values = result.getResponse().getHeaders(name);
+ assertThat("Response header '" + name + "'", values, matcher);
};
}
@@ -86,12 +76,7 @@ public class HeaderResultMatchers {
* Assert the primary value of the response header as a String value.
*/
public ResultMatcher string(final String name, final String value) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- assertEquals("Response header " + name, value, result.getResponse().getHeader(name));
- }
- };
+ return result -> assertEquals("Response header '" + name + "'", value, result.getResponse().getHeader(name));
}
/**
@@ -99,12 +84,9 @@ public class HeaderResultMatchers {
* @since 4.3
*/
public ResultMatcher stringValues(final String name, final String... values) {
- return new ResultMatcher() {
- @Override
- public void match(MvcResult result) {
- List