Simplify assertions within MockMvc internals

This commit is contained in:
Sam Brannen
2019-08-07 17:32:02 +02:00
parent d32cb7dac3
commit 50e5334378
5 changed files with 23 additions and 37 deletions

View File

@@ -31,6 +31,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;
/**
@@ -77,10 +78,8 @@ public class ContentResultMatchers {
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));
};
}
@@ -99,12 +98,10 @@ public class ContentResultMatchers {
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));
};
}

View File

@@ -20,12 +20,12 @@ import javax.servlet.http.Cookie;
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;
/**
@@ -208,9 +208,7 @@ public class CookieResultMatchers {
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;
}

View File

@@ -22,7 +22,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.assertTrue;
import static org.springframework.test.util.AssertionErrors.assertNotNull;
/**
* Factory for "output" flash attribute assertions.
@@ -64,7 +64,7 @@ public class FlashAttributeResultMatchers {
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));
}
};
}

View File

@@ -31,6 +31,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
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;
@@ -65,7 +66,7 @@ public class 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)) {
@@ -148,10 +149,7 @@ public class HandlerResultMatchers {
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;
}

View File

@@ -28,8 +28,8 @@ 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.assertNotNull;
import static org.springframework.test.util.AssertionErrors.assertTrue;
import static org.springframework.test.util.AssertionErrors.fail;
/**
* Factory for assertions on the model.
@@ -38,6 +38,7 @@ import static org.springframework.test.util.AssertionErrors.fail;
* {@link MockMvcResultMatchers#model}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.2
*/
public class ModelResultMatchers {
@@ -78,7 +79,7 @@ public class ModelResultMatchers {
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));
}
};
}
@@ -159,11 +160,9 @@ public class ModelResultMatchers {
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);
};
}
@@ -177,11 +176,9 @@ public class ModelResultMatchers {
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);
};
@@ -239,17 +236,13 @@ public class ModelResultMatchers {
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;
}