Search in sources :

Example 26 with ResultMatcher

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);
    }
}
Also used : StubMvcResult(org.springframework.test.web.servlet.StubMvcResult) HttpStatus(org.springframework.http.HttpStatus) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) StubMvcResult(org.springframework.test.web.servlet.StubMvcResult) MvcResult(org.springframework.test.web.servlet.MvcResult) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 27 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 28 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 29 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)

Example 30 with ResultMatcher

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

Aggregations

MvcResult (org.springframework.test.web.servlet.MvcResult)48 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)48 Cookie (javax.servlet.http.Cookie)15 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)13 ModelAndView (org.springframework.web.servlet.ModelAndView)11 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 Test (org.junit.Test)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 Errors (org.springframework.validation.Errors)1 MethodInvocationInfo (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.MethodInvocationInfo)1