Browse Source

Simplify assertions within MockMvc internals

pull/23435/head
Sam Brannen 5 years ago
parent
commit
50e5334378
  1. 17
      spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java
  2. 6
      spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java
  3. 4
      spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java
  4. 8
      spring-test/src/main/java/org/springframework/test/web/servlet/result/HandlerResultMatchers.java
  5. 25
      spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java

17
spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java

@ -31,6 +31,7 @@ import org.springframework.test.web.servlet.ResultMatcher; @@ -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 { @@ -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 { @@ -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));
};
}

6
spring-test/src/main/java/org/springframework/test/web/servlet/result/CookieResultMatchers.java

@ -20,12 +20,12 @@ import javax.servlet.http.Cookie; @@ -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 { @@ -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;
}

4
spring-test/src/main/java/org/springframework/test/web/servlet/result/FlashAttributeResultMatchers.java

@ -22,7 +22,7 @@ import org.springframework.test.web.servlet.ResultMatcher; @@ -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 { @@ -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));
}
};
}

8
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 @@ -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 { @@ -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 { @@ -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;
}

25
spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java

@ -28,8 +28,8 @@ import org.springframework.web.servlet.ModelAndView; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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;
}

Loading…
Cancel
Save