Skip to content

Commit

Permalink
Simplify assertions within MockMvc internals
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Aug 7, 2019
1 parent d32cb7d commit 50e5334
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 37 deletions.
Expand Up @@ -31,6 +31,7 @@

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;

/**
Expand Down Expand Up @@ -77,10 +78,8 @@ public ResultMatcher contentType(String contentType) {
public ResultMatcher contentType(MediaType contentType) {
return result -> {
String actual = result.getResponse().getContentType();
assertTrue("Content type not set", actual != null);
if (actual != null) {
assertEquals("Content type", contentType, MediaType.parseMediaType(actual));
}
assertNotNull("Content type not set", actual);
assertEquals("Content type", contentType, MediaType.parseMediaType(actual));
};
}

Expand All @@ -99,12 +98,10 @@ public ResultMatcher contentTypeCompatibleWith(String contentType) {
public ResultMatcher contentTypeCompatibleWith(MediaType contentType) {
return result -> {
String actual = result.getResponse().getContentType();
assertTrue("Content type not set", actual != null);
if (actual != null) {
MediaType actualContentType = MediaType.parseMediaType(actual);
assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]",
actualContentType.isCompatibleWith(contentType));
}
assertNotNull("Content type not set", actual);
MediaType actualContentType = MediaType.parseMediaType(actual);
assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]",
actualContentType.isCompatibleWith(contentType));
};
}

Expand Down
Expand Up @@ -20,12 +20,12 @@

import org.hamcrest.Matcher;

import org.springframework.test.util.AssertionErrors;
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.assertNotNull;
import static org.springframework.test.util.AssertionErrors.assertTrue;

/**
Expand Down Expand Up @@ -208,9 +208,7 @@ public ResultMatcher httpOnly(String name, boolean httpOnly) {

private static Cookie getCookie(MvcResult result, String name) {
Cookie cookie = result.getResponse().getCookie(name);
if (cookie == null) {
AssertionErrors.fail("No cookie with name '" + name + "'");
}
assertNotNull("No cookie with name '" + name + "'", cookie);
return cookie;
}

Expand Down
Expand Up @@ -22,7 +22,7 @@

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.assertNotNull;

/**
* Factory for "output" flash attribute assertions.
Expand Down Expand Up @@ -64,7 +64,7 @@ public <T> ResultMatcher attribute(String name, Object value) {
public <T> ResultMatcher attributeExists(String... names) {
return result -> {
for (String name : names) {
assertTrue("Flash attribute '" + name + "' does not exist", result.getFlashMap().get(name) != null);
assertNotNull("Flash attribute '" + name + "' does not exist", result.getFlashMap().get(name));
}
};
}
Expand Down
Expand Up @@ -31,6 +31,7 @@

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.fail;

Expand Down Expand Up @@ -65,7 +66,7 @@ protected HandlerResultMatchers() {
public ResultMatcher handlerType(Class<?> type) {
return result -> {
Object handler = result.getHandler();
assertTrue("No handler", handler != null);
assertNotNull("No handler", handler);
if (handler != null) {
Class<?> actual = handler.getClass();
if (HandlerMethod.class.isInstance(handler)) {
Expand Down Expand Up @@ -148,10 +149,7 @@ public ResultMatcher method(Method method) {

private static HandlerMethod getHandlerMethod(MvcResult result) {
Object handler = result.getHandler();
assertTrue("No handler", handler != null);
if (!(handler instanceof HandlerMethod)) {
fail("Not a HandlerMethod: " + handler);
}
assertTrue("Not a HandlerMethod: " + handler, handler instanceof HandlerMethod);
return (HandlerMethod) handler;
}

Expand Down
Expand Up @@ -28,8 +28,8 @@

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.fail;

/**
* Factory for assertions on the model.
Expand All @@ -38,6 +38,7 @@
* {@link MockMvcResultMatchers#model}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.2
*/
public class ModelResultMatchers {
Expand Down Expand Up @@ -78,7 +79,7 @@ public ResultMatcher attributeExists(String... names) {
return result -> {
ModelAndView mav = getModelAndView(result);
for (String name : names) {
assertTrue("Model attribute '" + name + "' does not exist", mav.getModel().get(name) != null);
assertNotNull("Model attribute '" + name + "' does not exist", mav.getModel().get(name));
}
};
}
Expand Down Expand Up @@ -159,11 +160,9 @@ public ResultMatcher attributeHasFieldErrorCode(String name, String fieldName, S
BindingResult result = getBindingResult(mav, name);
assertTrue("No errors for attribute '" + name + "'", result.hasErrors());
FieldError fieldError = result.getFieldError(fieldName);
if (fieldError == null) {
fail("No errors for field '" + fieldName + "' of attribute '" + name + "'");
}
assertNotNull("No errors for field '" + fieldName + "' of attribute '" + name + "'", fieldError);
String code = fieldError.getCode();
assertTrue("Expected error code '" + error + "' but got '" + code + "'", error.equals(code));
assertEquals("Field error code", error, code);
};
}

Expand All @@ -177,11 +176,9 @@ public <T> ResultMatcher attributeHasFieldErrorCode(String name, String fieldNam
return mvcResult -> {
ModelAndView mav = getModelAndView(mvcResult);
BindingResult result = getBindingResult(mav, name);
assertTrue("No errors for attribute: [" + name + "]", result.hasErrors());
assertTrue("No errors for attribute '" + name + "'", result.hasErrors());
FieldError fieldError = result.getFieldError(fieldName);
if (fieldError == null) {
fail("No errors for field '" + fieldName + "' of attribute '" + name + "'");
}
assertNotNull("No errors for field '" + fieldName + "' of attribute '" + name + "'", fieldError);
String code = fieldError.getCode();
assertThat("Field name '" + fieldName + "' of attribute '" + name + "'", code, matcher);
};
Expand Down Expand Up @@ -239,17 +236,13 @@ public <T> ResultMatcher size(int size) {

private ModelAndView getModelAndView(MvcResult mvcResult) {
ModelAndView mav = mvcResult.getModelAndView();
if (mav == null) {
fail("No ModelAndView found");
}
assertNotNull("No ModelAndView found", mav);
return mav;
}

private BindingResult getBindingResult(ModelAndView mav, String name) {
BindingResult result = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + name);
if (result == null) {
fail("No BindingResult for attribute: " + name);
}
assertNotNull("No BindingResult for attribute: " + name, result);
return result;
}

Expand Down

0 comments on commit 50e5334

Please sign in to comment.