Polish MockMvcResultMatchers[Tests]

This commit is contained in:
Sam Brannen
2016-10-12 21:57:47 +02:00
parent 8cf1933148
commit 509796a4b5
2 changed files with 47 additions and 59 deletions

View File

@@ -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.
* <p>This methods accepts only exact matches.
* <p>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.
* <p>This methods accepts {@link org.springframework.util.AntPathMatcher}
* expressions.
* @param urlPattern an AntPath expression to match against
* <p>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.
* <p>This methods accepts only exact matches.
* <p>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.
* <p>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()));
};
}