From 509796a4b55e85b1e7efb4355908c2eff0b3d44d Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 12 Oct 2016 21:57:47 +0200 Subject: [PATCH] Polish MockMvcResultMatchers[Tests] --- .../servlet/result/MockMvcResultMatchers.java | 61 +++++++------------ .../result/MockMvcResultMatchersTests.java | 45 ++++++++------ 2 files changed, 47 insertions(+), 59 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java index a12baf949f..7f5f4d29ab 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java @@ -21,7 +21,6 @@ import javax.xml.xpath.XPathExpressionException; import org.hamcrest.Matcher; -import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.util.AntPathMatcher; @@ -81,67 +80,51 @@ public abstract class MockMvcResultMatchers { /** * Asserts the request was forwarded to the given URL. - *

This methods accepts only exact matches. + *

This method accepts only exact matches. * @param expectedUrl the exact URL expected */ - public static ResultMatcher forwardedUrl(final String expectedUrl) { - return new ResultMatcher() { - @Override - public void match(MvcResult result) { - assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl()); - } - }; + public static ResultMatcher forwardedUrl(String expectedUrl) { + return result -> assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl()); } /** * Asserts the request was forwarded to the given URL. - *

This methods accepts {@link org.springframework.util.AntPathMatcher} - * expressions. - * @param urlPattern an AntPath expression to match against + *

This method accepts {@link org.springframework.util.AntPathMatcher} + * patterns. + * @param urlPattern an AntPath pattern to match against * @since 4.0 * @see org.springframework.util.AntPathMatcher */ - public static ResultMatcher forwardedUrlPattern(final String urlPattern) { - return new ResultMatcher() { - @Override - public void match(MvcResult result) { - assertTrue("AntPath expression", pathMatcher.isPattern(urlPattern)); - assertTrue("Forwarded URL does not match the expected URL pattern", - pathMatcher.match(urlPattern, result.getResponse().getForwardedUrl())); - } + public static ResultMatcher forwardedUrlPattern(String urlPattern) { + return result -> { + assertTrue("AntPath pattern", pathMatcher.isPattern(urlPattern)); + assertTrue("Forwarded URL does not match the expected URL pattern", + pathMatcher.match(urlPattern, result.getResponse().getForwardedUrl())); }; } /** * Asserts the request was redirected to the given URL. - *

This methods accepts only exact matches. + *

This method accepts only exact matches. * @param expectedUrl the exact URL expected */ - public static ResultMatcher redirectedUrl(final String expectedUrl) { - return new ResultMatcher() { - @Override - public void match(MvcResult result) { - assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl()); - } - }; + public static ResultMatcher redirectedUrl(String expectedUrl) { + return result -> assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl()); } /** * Asserts the request was redirected to the given URL. *

This method accepts {@link org.springframework.util.AntPathMatcher} - * expressions. - * @param expectedUrl an AntPath expression to match against - * @see org.springframework.util.AntPathMatcher + * patterns. + * @param urlPattern an AntPath pattern to match against * @since 4.0 + * @see org.springframework.util.AntPathMatcher */ - public static ResultMatcher redirectedUrlPattern(final String expectedUrl) { - return new ResultMatcher() { - @Override - public void match(MvcResult result) { - assertTrue("AntPath expression",pathMatcher.isPattern(expectedUrl)); - assertTrue("Redirected URL", - pathMatcher.match(expectedUrl, result.getResponse().getRedirectedUrl())); - } + public static ResultMatcher redirectedUrlPattern(String urlPattern) { + return result -> { + assertTrue("AntPath pattern", pathMatcher.isPattern(urlPattern)); + assertTrue("Redirected URL does not match the expected URL pattern", + pathMatcher.match(urlPattern, result.getResponse().getRedirectedUrl())); }; } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java index 6d2eb4b92b..a4e4563843 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/MockMvcResultMatchersTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.servlet.result; import org.junit.Test; @@ -20,45 +21,49 @@ import org.junit.Test; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.StubMvcResult; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + /** + * Unit tests for {@link MockMvcResultMatchers}. + * * @author Brian Clozel + * @author Sam Brannen */ public class MockMvcResultMatchersTests { @Test - public void testRedirect() throws Exception { - MockMvcResultMatchers.redirectedUrl("/resource/1") - .match(getRedirectedUrlStubMvcResult("/resource/1")); + public void redirect() throws Exception { + redirectedUrl("/resource/1").match(getRedirectedUrlStubMvcResult("/resource/1")); } @Test - public void testRedirectPattern() throws Exception { - MockMvcResultMatchers.redirectedUrlPattern("/resource/*") - .match(getRedirectedUrlStubMvcResult("/resource/1")); + public void redirectWithMatchingPattern() throws Exception { + redirectedUrlPattern("/resource/*").match(getRedirectedUrlStubMvcResult("/resource/1")); } - @Test( expected = java.lang.AssertionError.class) - public void testFailRedirectPattern() throws Exception { - MockMvcResultMatchers.redirectedUrlPattern("/resource/") - .match(getRedirectedUrlStubMvcResult("/resource/1")); + @Test(expected = AssertionError.class) + public void redirectWithNonMatchingPattern() throws Exception { + redirectedUrlPattern("/resource/").match(getRedirectedUrlStubMvcResult("/resource/1")); } @Test - public void testForward() throws Exception { - MockMvcResultMatchers.forwardedUrl("/api/resource/1") - .match(getForwardedUrlStubMvcResult("/api/resource/1")); + public void forward() throws Exception { + forwardedUrl("/api/resource/1").match(getForwardedUrlStubMvcResult("/api/resource/1")); } @Test - public void testForwardEscapedChars() throws Exception { - MockMvcResultMatchers.forwardedUrl("/api/resource/1?arg=value") - .match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value")); + public void forwardWithQueryString() throws Exception { + forwardedUrl("/api/resource/1?arg=value").match(getForwardedUrlStubMvcResult("/api/resource/1?arg=value")); } @Test - public void testForwardPattern() throws Exception { - MockMvcResultMatchers.forwardedUrlPattern("/api/**/?") - .match(getForwardedUrlStubMvcResult("/api/resource/1")); + public void forwardWithMatchingPattern() throws Exception { + forwardedUrlPattern("/api/**/?").match(getForwardedUrlStubMvcResult("/api/resource/1")); + } + + @Test(expected = AssertionError.class) + public void forwardWithNonMatchingPattern() throws Exception { + forwardedUrlPattern("/resource/").match(getForwardedUrlStubMvcResult("/resource/1")); } private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {