Search in sources :

Example 31 with ResultMatcher

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

the class ModelResultMatchers method attributeHasErrors.

/**
	 * Assert the given model attribute(s) have errors.
	 */
public ResultMatcher attributeHasErrors(final String... names) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult mvcResult) throws Exception {
            ModelAndView mav = getModelAndView(mvcResult);
            for (String name : names) {
                BindingResult result = getBindingResult(mav, name);
                assertTrue("No errors for attribute [" + name + "]", result.hasErrors());
            }
        }
    };
}
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 32 with ResultMatcher

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

the class ModelResultMatchers method attributeHasFieldErrors.

/**
	 * Assert the given model attribute field(s) have errors.
	 */
public ResultMatcher attributeHasFieldErrors(final String name, final String... fieldNames) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult mvcResult) throws Exception {
            ModelAndView mav = getModelAndView(mvcResult);
            BindingResult result = getBindingResult(mav, name);
            assertTrue("No errors for attribute: [" + name + "]", result.hasErrors());
            for (final String fieldName : fieldNames) {
                boolean hasFieldErrors = result.hasFieldErrors(fieldName);
                assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors);
            }
        }
    };
}
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 33 with ResultMatcher

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

the class CookieResultMatchers method value.

/**
	 * Assert a cookie value with the given Hamcrest {@link Matcher}.
	 */
public ResultMatcher value(final String name, final Matcher<? super String> matcher) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) {
            Cookie cookie = result.getResponse().getCookie(name);
            assertTrue("Response cookie not found: " + name, cookie != null);
            assertThat("Response cookie", cookie.getValue(), matcher);
        }
    };
}
Also used : Cookie(javax.servlet.http.Cookie) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 34 with ResultMatcher

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

the class CookieResultMatchers method domain.

/**
	 * Assert a cookie's domain value.
	 */
public ResultMatcher domain(final String name, final String domain) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            Cookie cookie = result.getResponse().getCookie(name);
            assertEquals("Response cookie domain", domain, cookie.getDomain());
        }
    };
}
Also used : Cookie(javax.servlet.http.Cookie) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 35 with ResultMatcher

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

the class CookieResultMatchers method version.

/**
	 * Assert a cookie's version value.
	 */
public ResultMatcher version(final String name, final int version) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            Cookie cookie = result.getResponse().getCookie(name);
            assertEquals("Response cookie version", version, cookie.getVersion());
        }
    };
}
Also used : Cookie(javax.servlet.http.Cookie) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

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