Search in sources :

Example 16 with ResultMatcher

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

Example 17 with ResultMatcher

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

the class RequestResultMatchers method asyncNotStarted.

/**
	 * Assert that asynchronous processing was not started.
	 * @see #asyncStarted()
	 */
public ResultMatcher asyncNotStarted() {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) {
            HttpServletRequest request = result.getRequest();
            assertEquals("Async started", false, request.isAsyncStarted());
        }
    };
}
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 18 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 with the given matcher.
	 * <p>This method can be used when a controller method returns {@link Callable}
	 * or {@link WebAsyncTask}.
	 */
public <T> ResultMatcher asyncResult(final Matcher<T> matcher) {
    return new ResultMatcher() {

        @Override
        @SuppressWarnings("unchecked")
        public void match(MvcResult result) {
            HttpServletRequest request = result.getRequest();
            assertAsyncStarted(request);
            assertThat("Async result", (T) result.getAsyncResult(), matcher);
        }
    };
}
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 19 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.
	 */
public ResultMatcher name(final String expectedViewName) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            ModelAndView mav = result.getModelAndView();
            assertTrue("No ModelAndView found", mav != null);
            assertEquals("View name", expectedViewName, mav.getViewName());
        }
    };
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 20 with ResultMatcher

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

the class XpathResultMatchers method number.

/**
	 * Evaluate the XPath and assert the {@link Double} value found with the
	 * given Hamcrest {@link Matcher}.
	 */
public ResultMatcher number(final Matcher<? super Double> matcher) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            MockHttpServletResponse response = result.getResponse();
            xpathHelper.assertNumber(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