use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.
the class StatusResultMatchersTests method testHttpStatusCodeResultMatchers.
@Test
public void testHttpStatusCodeResultMatchers() throws Exception {
List<AssertionError> failures = new ArrayList<>();
for (HttpStatus status : HttpStatus.values()) {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setStatus(status.value());
MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
try {
Method method = getMethodForHttpStatus(status);
ResultMatcher matcher = (ResultMatcher) ReflectionUtils.invokeMethod(method, this.matchers);
try {
matcher.match(mvcResult);
} catch (AssertionError error) {
failures.add(error);
}
} catch (Exception ex) {
throw new Exception("Failed to obtain ResultMatcher for status " + status, ex);
}
}
if (!failures.isEmpty()) {
fail("Failed status codes: " + failures);
}
}
use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.
the class RequestResultMatchers method asyncStarted.
/**
* Assert whether asynchronous processing started, usually as a result of a
* controller method returning {@link Callable} or {@link DeferredResult}.
* <p>The test will await the completion of a {@code Callable} so that
* {@link #asyncResult(Matcher)} can be used to assert the resulting value.
* Neither a {@code Callable} nor a {@code DeferredResult} will complete
* processing all the way since a {@link MockHttpServletRequest} does not
* perform asynchronous dispatches.
*/
public ResultMatcher asyncStarted() {
return new ResultMatcher() {
@Override
public void match(MvcResult result) {
HttpServletRequest request = result.getRequest();
assertAsyncStarted(request);
}
};
}
use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.
the class RequestResultMatchers method asyncResult.
/**
* Assert the result from asynchronous processing.
* <p>This method can be used when a controller method returns {@link Callable}
* or {@link WebAsyncTask}. The value matched is the value returned from the
* {@code Callable} or the exception raised.
*/
public <T> ResultMatcher asyncResult(final Object expectedResult) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) {
HttpServletRequest request = result.getRequest();
assertAsyncStarted(request);
assertEquals("Async result", expectedResult, result.getAsyncResult());
}
};
}
use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.
the class ViewResultMatchers method name.
/**
* Assert the selected view name with the given Hamcrest {@link Matcher}.
*/
public ResultMatcher name(final Matcher<? super String> matcher) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
ModelAndView mav = result.getModelAndView();
assertTrue("No ModelAndView found", mav != null);
assertThat("View name", mav.getViewName(), matcher);
}
};
}
use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.
the class XpathResultMatchers method string.
/**
* Apply the XPath and assert the {@link String} value found with the given
* Hamcrest {@link Matcher}.
*/
public ResultMatcher string(final Matcher<? super String> matcher) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
MockHttpServletResponse response = result.getResponse();
xpathHelper.assertString(response.getContentAsByteArray(), getDefinedEncoding(response), matcher);
}
};
}
Aggregations