Polish MockMvc result matchers and tests

This commit is contained in:
Sam Brannen
2019-10-23 14:03:44 +02:00
parent 82f64f6a8d
commit eabf357640
13 changed files with 176 additions and 126 deletions

View File

@@ -71,6 +71,30 @@ public abstract class AssertionErrors {
}
}
/**
* Assert the given condition is {@code false} and raise an
* {@link AssertionError} otherwise.
* @param message a message that describes the reason for the failure
* @param condition the condition to test for
* @since 5.2.1
*/
public static void assertFalse(String message, boolean condition) {
if (condition) {
fail(message);
}
}
/**
* Assert that the given object is {@code null} and raise an
* {@link AssertionError} otherwise.
* @param message a message that describes the reason for the failure
* @param object the object to check
* @since 5.2.1
*/
public static void assertNull(String message, @Nullable Object object) {
assertTrue(message, object == null);
}
/**
* Assert that the given object is not {@code null} and raise an
* {@link AssertionError} otherwise.

View File

@@ -26,7 +26,7 @@ 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.assertNotNull;
import static org.springframework.test.util.AssertionErrors.assertTrue;
import static org.springframework.test.util.AssertionErrors.assertNull;
/**
* Factory for response cookie assertions.
@@ -83,7 +83,7 @@ public class CookieResultMatchers {
public ResultMatcher doesNotExist(String name) {
return result -> {
Cookie cookie = result.getResponse().getCookie(name);
assertTrue("Unexpected cookie with name '" + name + "'", cookie == null);
assertNull("Unexpected cookie with name '" + name + "'", cookie);
};
}
@@ -98,7 +98,7 @@ public class CookieResultMatchers {
}
/**
* Assert a cookie's maxAge value.
* Assert a cookie's maxAge.
*/
public ResultMatcher maxAge(String name, int maxAge) {
return result -> {
@@ -108,7 +108,7 @@ public class CookieResultMatchers {
}
/**
* Assert a cookie path with a Hamcrest {@link Matcher}.
* Assert a cookie's path with a Hamcrest {@link Matcher}.
*/
public ResultMatcher path(String name, Matcher<? super String> matcher) {
return result -> {
@@ -117,6 +117,9 @@ public class CookieResultMatchers {
};
}
/**
* Assert a cookie's path.
*/
public ResultMatcher path(String name, String path) {
return result -> {
Cookie cookie = getCookie(result, name);
@@ -135,7 +138,7 @@ public class CookieResultMatchers {
}
/**
* Assert a cookie's domain value.
* Assert a cookie's domain.
*/
public ResultMatcher domain(String name, String domain) {
return result -> {
@@ -155,7 +158,7 @@ public class CookieResultMatchers {
}
/**
* Assert a cookie's comment value.
* Assert a cookie's comment.
*/
public ResultMatcher comment(String name, String comment) {
return result -> {
@@ -175,7 +178,7 @@ public class CookieResultMatchers {
}
/**
* Assert a cookie's version value.
* Assert a cookie's version.
*/
public ResultMatcher version(String name, int version) {
return result -> {

View File

@@ -73,7 +73,7 @@ public class FlashAttributeResultMatchers {
* Assert the number of flash attributes.
*/
public <T> ResultMatcher attributeCount(int count) {
return result -> assertEquals("FlashMap size must be " + count, count, result.getFlashMap().size());
return result -> assertEquals("FlashMap size", count, result.getFlashMap().size());
}
}

View File

@@ -27,6 +27,7 @@ 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.assertFalse;
import static org.springframework.test.util.AssertionErrors.assertNotNull;
import static org.springframework.test.util.AssertionErrors.assertTrue;
@@ -103,8 +104,8 @@ public class HeaderResultMatchers {
* @since 4.0
*/
public ResultMatcher doesNotExist(String name) {
return result -> assertTrue("Response should not contain header '" + name + "'",
!result.getResponse().containsHeader(name));
return result -> assertFalse("Response should not contain header '" + name + "'",
result.getResponse().containsHeader(name));
}
/**

View File

@@ -28,7 +28,9 @@ import org.springframework.web.servlet.ModelAndView;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertFalse;
import static org.springframework.test.util.AssertionErrors.assertNotNull;
import static org.springframework.test.util.AssertionErrors.assertNull;
import static org.springframework.test.util.AssertionErrors.assertTrue;
/**
@@ -91,7 +93,7 @@ public class ModelResultMatchers {
return result -> {
ModelAndView mav = getModelAndView(result);
for (String name : names) {
assertTrue("Model attribute '" + name + "' exists", mav.getModel().get(name) == null);
assertNull("Model attribute '" + name + "' exists", mav.getModel().get(name));
}
};
}
@@ -103,7 +105,7 @@ public class ModelResultMatchers {
return result -> {
ModelAndView mav = getModelAndView(result);
Errors errors = getBindingResult(mav, name);
assertEquals("Binding/validation error count for attribute '" + name + "', ",
assertEquals("Binding/validation error count for attribute '" + name + "',",
expectedCount, errors.getErrorCount());
};
}
@@ -129,8 +131,8 @@ public class ModelResultMatchers {
ModelAndView mav = getModelAndView(mvcResult);
for (String name : names) {
BindingResult result = getBindingResult(mav, name);
assertTrue("Unexpected errors for attribute '" + name + "': " + result.getAllErrors(),
!result.hasErrors());
assertFalse("Unexpected errors for attribute '" + name + "': " + result.getAllErrors(),
result.hasErrors());
}
};
}
@@ -212,7 +214,7 @@ public class ModelResultMatchers {
ModelAndView mav = getModelAndView(result);
for (Object value : mav.getModel().values()) {
if (value instanceof Errors) {
assertTrue("Unexpected binding/validation errors: " + value, !((Errors) value).hasErrors());
assertFalse("Unexpected binding/validation errors: " + value, ((Errors) value).hasErrors());
}
}
};

View File

@@ -31,6 +31,8 @@ import org.springframework.web.context.request.async.WebAsyncTask;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertFalse;
import static org.springframework.test.util.AssertionErrors.assertNull;
import static org.springframework.test.util.AssertionErrors.assertTrue;
/**
@@ -57,16 +59,15 @@ public class RequestResultMatchers {
* Assert whether asynchronous processing started, usually as a result of a
* controller method returning {@link Callable} or {@link DeferredResult}.
* <p>The test will await the completion of a {@code Callable} so that
* {@link #asyncResult(Matcher)} can be used to assert the resulting value.
* Neither a {@code Callable} nor a {@code DeferredResult} will complete
* {@link #asyncResult(Matcher)} or {@link #asyncResult(Object)} can be used
* to assert the resulting value.
* <p>Neither a {@code Callable} nor a {@code DeferredResult} will complete
* processing all the way since a {@link MockHttpServletRequest} does not
* perform asynchronous dispatches.
* @see #asyncNotStarted()
*/
public ResultMatcher asyncStarted() {
return result -> {
HttpServletRequest request = result.getRequest();
assertAsyncStarted(request);
};
return result -> assertAsyncStarted(result.getRequest());
}
/**
@@ -74,10 +75,7 @@ public class RequestResultMatchers {
* @see #asyncStarted()
*/
public ResultMatcher asyncNotStarted() {
return result -> {
HttpServletRequest request = result.getRequest();
assertEquals("Async started", false, request.isAsyncStarted());
};
return result -> assertFalse("Async started", result.getRequest().isAsyncStarted());
}
/**
@@ -160,13 +158,13 @@ public class RequestResultMatchers {
HttpSession session = result.getRequest().getSession();
Assert.state(session != null, "No HttpSession");
for (String name : names) {
assertTrue("Session attribute '" + name + "' exists", session.getAttribute(name) == null);
assertNull("Session attribute '" + name + "' exists", session.getAttribute(name));
}
};
}
private static void assertAsyncStarted(HttpServletRequest request) {
assertEquals("Async started", true, request.isAsyncStarted());
assertTrue("Async not started", request.isAsyncStarted());
}
}