use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.
the class HandlerResultMatchers method methodCall.
/**
* Assert the controller method used to process the request.
* <p>The expected method is specified through a "mock" controller method
* invocation similar to {@link MvcUriComponentsBuilder#fromMethodCall(Object)}.
* <p>For example, given this controller:
* <pre class="code">
* @RestController
* public class SimpleController {
*
* @RequestMapping("/")
* public ResponseEntity<Void> handle() {
* return ResponseEntity.ok().build();
* }
* }
* </pre>
* <p>A test that has statically imported {@link MvcUriComponentsBuilder#on}
* can be performed as follows:
* <pre class="code">
* mockMvc.perform(get("/"))
* .andExpect(handler().methodCall(on(SimpleController.class).handle()));
* </pre>
*
* @param obj either the value returned from a "mock" controller invocation
* or the "mock" controller itself after an invocation
*/
public ResultMatcher methodCall(final Object obj) {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
if (!MethodInvocationInfo.class.isInstance(obj)) {
fail(String.format("The supplied object [%s] is not an instance of %s. " + "Ensure that you invoke the handler method via MvcUriComponentsBuilder.on().", obj, MethodInvocationInfo.class.getName()));
}
MethodInvocationInfo invocationInfo = (MethodInvocationInfo) obj;
Method expected = invocationInfo.getControllerMethod();
Method actual = getHandlerMethod(result).getMethod();
assertEquals("Handler method", expected, actual);
}
};
}
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