Search in sources :

Example 51 with ResultMatcher

use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.

the class HandlerResultMatchers method methodName.

/**
	 * Assert the name of the controller method used to process the request.
	 */
public ResultMatcher methodName(final String name) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            HandlerMethod handlerMethod = getHandlerMethod(result);
            assertEquals("Handler method", name, handlerMethod.getMethod().getName());
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 52 with ResultMatcher

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">
	 * &#064;RestController
	 * public class SimpleController {
	 *
	 *     &#064;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);
        }
    };
}
Also used : MethodInvocationInfo(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.MethodInvocationInfo) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 53 with ResultMatcher

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);
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 54 with ResultMatcher

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());
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 55 with ResultMatcher

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);
        }
    };
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Aggregations

ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)74 MvcResult (org.springframework.test.web.servlet.MvcResult)69 Test (org.junit.jupiter.api.Test)22 Cookie (javax.servlet.http.Cookie)15 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)13 ModelAndView (org.springframework.web.servlet.ModelAndView)11 Test (org.junit.Test)5 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)5 BindingResult (org.springframework.validation.BindingResult)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 HandlerMethod (org.springframework.web.method.HandlerMethod)4 Method (java.lang.reflect.Method)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 AssertionErrors (org.springframework.test.util.AssertionErrors)1 StubMvcResult (org.springframework.test.web.servlet.StubMvcResult)1