From 50e53343789fe985b09e9be83481005074f716ee Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 7 Aug 2019 17:32:02 +0200 Subject: [PATCH] Simplify assertions within MockMvc internals --- .../servlet/result/ContentResultMatchers.java | 17 ++++++------- .../servlet/result/CookieResultMatchers.java | 6 ++--- .../result/FlashAttributeResultMatchers.java | 4 +-- .../servlet/result/HandlerResultMatchers.java | 8 +++--- .../servlet/result/ModelResultMatchers.java | 25 +++++++------------ 5 files changed, 23 insertions(+), 37 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java index c3510b2970..f16bcc326a 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java @@ -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)); }; } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java index 9c7eea203e..792753db9c 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java @@ -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; } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java index ab3571a5e0..72b6db0a29 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java @@ -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 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)); } }; } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java index 750e0197bd..bcd5fc0de4 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java @@ -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; } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java index 813889d41b..eff1666f80 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java @@ -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; }