|
|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
|
@ -30,6 +30,7 @@ import static org.springframework.test.util.AssertionErrors.*;
@@ -30,6 +30,7 @@ import static org.springframework.test.util.AssertionErrors.*;
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Factory for assertions on the model. |
|
|
|
|
* |
|
|
|
|
* <p>An instance of this class is typically accessed via |
|
|
|
|
* {@link MockMvcResultMatchers#model}. |
|
|
|
|
* |
|
|
|
|
@ -49,14 +50,11 @@ public class ModelResultMatchers {
@@ -49,14 +50,11 @@ public class ModelResultMatchers {
|
|
|
|
|
/** |
|
|
|
|
* Assert a model attribute value with the given Hamcrest {@link Matcher}. |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
public <T> ResultMatcher attribute(final String name, final Matcher<T> matcher) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
assertThat("Model attribute '" + name + "'", (T) mav.getModel().get(name), matcher); |
|
|
|
|
} |
|
|
|
|
return result -> { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
assertThat("Model attribute '" + name + "'", (T) mav.getModel().get(name), matcher); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -64,12 +62,9 @@ public class ModelResultMatchers {
@@ -64,12 +62,9 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert a model attribute value. |
|
|
|
|
*/ |
|
|
|
|
public ResultMatcher attribute(final String name, final Object value) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
assertEquals("Model attribute '" + name + "'", value, mav.getModel().get(name)); |
|
|
|
|
} |
|
|
|
|
return result -> { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
assertEquals("Model attribute '" + name + "'", value, mav.getModel().get(name)); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -77,13 +72,10 @@ public class ModelResultMatchers {
@@ -77,13 +72,10 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert the given model attributes exist. |
|
|
|
|
*/ |
|
|
|
|
public ResultMatcher attributeExists(final String... names) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
for (String name : names) { |
|
|
|
|
assertTrue("Model attribute '" + name + "' does not exist", mav.getModel().get(name) != null); |
|
|
|
|
} |
|
|
|
|
return result -> { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
for (String name : names) { |
|
|
|
|
assertTrue("Model attribute '" + name + "' does not exist", mav.getModel().get(name) != null); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
@ -92,29 +84,23 @@ public class ModelResultMatchers {
@@ -92,29 +84,23 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert the given model attributes do not exist |
|
|
|
|
*/ |
|
|
|
|
public ResultMatcher attributeDoesNotExist(final String... names) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
for (String name : names) { |
|
|
|
|
assertTrue("Model attribute '" + name + "' exists", mav.getModel().get(name) == null); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
return result -> { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
for (String name : names) { |
|
|
|
|
assertTrue("Model attribute '" + name + "' exists", mav.getModel().get(name) == null); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Assert the given model attribute(s) have errors. |
|
|
|
|
*/ |
|
|
|
|
public ResultMatcher attributeErrorCount(final String name, final int expectedCount) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
Errors errors = getBindingResult(mav, name); |
|
|
|
|
assertEquals("Binding/validation error count for attribute [" + name + "], ", |
|
|
|
|
expectedCount, errors.getErrorCount()); |
|
|
|
|
} |
|
|
|
|
return result -> { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
Errors errors = getBindingResult(mav, name); |
|
|
|
|
assertEquals("Binding/validation error count for attribute '" + name + "', ", |
|
|
|
|
expectedCount, errors.getErrorCount()); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -122,14 +108,11 @@ public class ModelResultMatchers {
@@ -122,14 +108,11 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert the given model attribute(s) have errors. |
|
|
|
|
*/ |
|
|
|
|
public ResultMatcher attributeHasErrors(final String... names) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult mvcResult) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
for (String name : names) { |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("No errors for attribute [" + name + "]", result.hasErrors()); |
|
|
|
|
} |
|
|
|
|
return mvcResult -> { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
for (String name : names) { |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("No errors for attribute '" + name + "'", result.hasErrors()); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
@ -138,14 +121,12 @@ public class ModelResultMatchers {
@@ -138,14 +121,12 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert the given model attribute(s) do not have errors. |
|
|
|
|
*/ |
|
|
|
|
public ResultMatcher attributeHasNoErrors(final String... names) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult mvcResult) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
for (String name : names) { |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("No errors for attribute [" + name + "]", !result.hasErrors()); |
|
|
|
|
} |
|
|
|
|
return mvcResult -> { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
for (String name : names) { |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("Unexpected errors for attribute '" + name + "': " + result.getAllErrors(), |
|
|
|
|
!result.hasErrors()); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
@ -154,16 +135,13 @@ public class ModelResultMatchers {
@@ -154,16 +135,13 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert the given model attribute field(s) have errors. |
|
|
|
|
*/ |
|
|
|
|
public ResultMatcher attributeHasFieldErrors(final String name, final String... fieldNames) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult mvcResult) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("No errors for attribute: [" + name + "]", result.hasErrors()); |
|
|
|
|
for (final String fieldName : fieldNames) { |
|
|
|
|
boolean hasFieldErrors = result.hasFieldErrors(fieldName); |
|
|
|
|
assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors); |
|
|
|
|
} |
|
|
|
|
return mvcResult -> { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("No errors for attribute '" + name + "'", result.hasErrors()); |
|
|
|
|
for (final String fieldName : fieldNames) { |
|
|
|
|
boolean hasFieldErrors = result.hasFieldErrors(fieldName); |
|
|
|
|
assertTrue("No errors for field '" + fieldName + "' of attribute '" + name + "'", hasFieldErrors); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
@ -173,18 +151,14 @@ public class ModelResultMatchers {
@@ -173,18 +151,14 @@ public class ModelResultMatchers {
|
|
|
|
|
* @since 4.1 |
|
|
|
|
*/ |
|
|
|
|
public ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName, final String error) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
public void match(MvcResult mvcResult) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("No errors for attribute: [" + name + "]", result.hasErrors()); |
|
|
|
|
|
|
|
|
|
boolean hasFieldErrors = result.hasFieldErrors(fieldName); |
|
|
|
|
assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors); |
|
|
|
|
|
|
|
|
|
String code = result.getFieldError(fieldName).getCode(); |
|
|
|
|
assertTrue("Expected error code '" + error + "' but got '" + code + "'", code.equals(error)); |
|
|
|
|
} |
|
|
|
|
return mvcResult -> { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("No errors for attribute '" + name + "'", result.hasErrors()); |
|
|
|
|
boolean hasFieldErrors = result.hasFieldErrors(fieldName); |
|
|
|
|
assertTrue("No errors for field '" + fieldName + "' of attribute '" + name + "'", hasFieldErrors); |
|
|
|
|
String code = result.getFieldError(fieldName).getCode(); |
|
|
|
|
assertTrue("Expected error code '" + error + "' but got '" + code + "'", code.equals(error)); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -195,19 +169,14 @@ public class ModelResultMatchers {
@@ -195,19 +169,14 @@ public class ModelResultMatchers {
|
|
|
|
|
public <T> ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName, |
|
|
|
|
final Matcher<? super String> matcher) { |
|
|
|
|
|
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult mvcResult) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("No errors for attribute: [" + name + "]", result.hasErrors()); |
|
|
|
|
|
|
|
|
|
boolean hasFieldErrors = result.hasFieldErrors(fieldName); |
|
|
|
|
assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors); |
|
|
|
|
|
|
|
|
|
String code = result.getFieldError(fieldName).getCode(); |
|
|
|
|
assertThat("Field name '" + fieldName + "' of attribute '" + name + "'", code, matcher); |
|
|
|
|
} |
|
|
|
|
return mvcResult -> { |
|
|
|
|
ModelAndView mav = getModelAndView(mvcResult); |
|
|
|
|
BindingResult result = getBindingResult(mav, name); |
|
|
|
|
assertTrue("No errors for attribute: [" + name + "]", result.hasErrors()); |
|
|
|
|
boolean hasFieldErrors = result.hasFieldErrors(fieldName); |
|
|
|
|
assertTrue("No errors for field '" + fieldName + "' of attribute '" + name + "'", hasFieldErrors); |
|
|
|
|
String code = result.getFieldError(fieldName).getCode(); |
|
|
|
|
assertThat("Field name '" + fieldName + "' of attribute '" + name + "'", code, matcher); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -215,12 +184,9 @@ public class ModelResultMatchers {
@@ -215,12 +184,9 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert the total number of errors in the model. |
|
|
|
|
*/ |
|
|
|
|
public <T> ResultMatcher errorCount(final int expectedCount) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
|
int actualCount = getErrorCount(getModelAndView(result).getModelMap()); |
|
|
|
|
assertEquals("Binding/validation error count", expectedCount, actualCount); |
|
|
|
|
} |
|
|
|
|
return result -> { |
|
|
|
|
int actualCount = getErrorCount(getModelAndView(result).getModelMap()); |
|
|
|
|
assertEquals("Binding/validation error count", expectedCount, actualCount); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -228,12 +194,9 @@ public class ModelResultMatchers {
@@ -228,12 +194,9 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert the model has errors. |
|
|
|
|
*/ |
|
|
|
|
public <T> ResultMatcher hasErrors() { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
|
int count = getErrorCount(getModelAndView(result).getModelMap()); |
|
|
|
|
assertTrue("Expected binding/validation errors", count != 0); |
|
|
|
|
} |
|
|
|
|
return result -> { |
|
|
|
|
int count = getErrorCount(getModelAndView(result).getModelMap()); |
|
|
|
|
assertTrue("Expected binding/validation errors", count != 0); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -241,15 +204,11 @@ public class ModelResultMatchers {
@@ -241,15 +204,11 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert the model has no errors. |
|
|
|
|
*/ |
|
|
|
|
public <T> ResultMatcher hasNoErrors() { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
for (Object value : mav.getModel().values()) { |
|
|
|
|
if (value instanceof Errors) { |
|
|
|
|
assertTrue("Unexpected binding/validation error(s) [" + value + "]", |
|
|
|
|
!((Errors) value).hasErrors()); |
|
|
|
|
} |
|
|
|
|
return result -> { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
for (Object value : mav.getModel().values()) { |
|
|
|
|
if (value instanceof Errors) { |
|
|
|
|
assertTrue("Unexpected binding/validation errors: " + value, !((Errors) value).hasErrors()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
@ -259,18 +218,15 @@ public class ModelResultMatchers {
@@ -259,18 +218,15 @@ public class ModelResultMatchers {
|
|
|
|
|
* Assert the number of model attributes. |
|
|
|
|
*/ |
|
|
|
|
public <T> ResultMatcher size(final int size) { |
|
|
|
|
return new ResultMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(MvcResult result) throws Exception { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
int actual = 0; |
|
|
|
|
for (String key : mav.getModel().keySet()) { |
|
|
|
|
if (!key.startsWith(BindingResult.MODEL_KEY_PREFIX)) { |
|
|
|
|
actual++; |
|
|
|
|
} |
|
|
|
|
return result -> { |
|
|
|
|
ModelAndView mav = getModelAndView(result); |
|
|
|
|
int actual = 0; |
|
|
|
|
for (String key : mav.getModel().keySet()) { |
|
|
|
|
if (!key.startsWith(BindingResult.MODEL_KEY_PREFIX)) { |
|
|
|
|
actual++; |
|
|
|
|
} |
|
|
|
|
assertEquals("Model size", size, actual); |
|
|
|
|
} |
|
|
|
|
assertEquals("Model size", size, actual); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|