use of org.springframework.test.web.servlet.MvcResult in project spring-framework by spring-projects.
the class HeaderResultMatchers method longValue.
/**
* Assert the primary value of the named response header as a {@code long}.
* <p>The {@link ResultMatcher} returned by this method throws an
* {@link AssertionError} if the response does not contain the specified
* header, or if the supplied {@code value} does not match the primary value.
*/
public ResultMatcher longValue(final String name, final long value) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) {
MockHttpServletResponse response = result.getResponse();
assertTrue("Response does not contain header " + name, response.containsHeader(name));
assertEquals("Response header " + name, value, Long.parseLong(response.getHeader(name)));
}
};
}
use of org.springframework.test.web.servlet.MvcResult in project spring-framework by spring-projects.
the class ModelResultMatchers method attributeHasNoErrors.
/**
* 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());
}
}
};
}
use of org.springframework.test.web.servlet.MvcResult in project spring-framework by spring-projects.
the class ModelResultMatchers method attribute.
/**
* Assert a model attribute value with the given Hamcrest {@link Matcher}.
*/
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);
}
};
}
use of org.springframework.test.web.servlet.MvcResult in project spring-framework by spring-projects.
the class ModelResultMatchers method size.
/**
* 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++;
}
}
assertEquals("Model size", size, actual);
}
};
}
use of org.springframework.test.web.servlet.MvcResult in project spring-framework by spring-projects.
the class ModelResultMatchers method attributeHasFieldErrorCode.
/**
* Assert a field error code for a model attribute using exact String match.
* @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));
}
};
}
Aggregations