Search in sources :

Example 1 with StubMvcResult

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

the class FlashAttributeResultMatchersTests method getStubMvcResult.

private StubMvcResult getStubMvcResult() {
    FlashMap flashMap = new FlashMap();
    flashMap.put("good", "good");
    StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, flashMap, null);
    return mvcResult;
}
Also used : StubMvcResult(org.springframework.test.web.servlet.StubMvcResult) FlashMap(org.springframework.web.servlet.FlashMap)

Example 2 with StubMvcResult

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

the class JsonPathResultMatchersTests method valueWithJsonPrefix.

@Test
public void valueWithJsonPrefix() throws Exception {
    String jsonPrefix = "prefix";
    StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
    new JsonPathResultMatchers("$.str").prefix(jsonPrefix).value("foo").match(result);
}
Also used : StubMvcResult(org.springframework.test.web.servlet.StubMvcResult) Test(org.junit.jupiter.api.Test)

Example 3 with StubMvcResult

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

the class JsonPathResultMatchersTests method valueWithJsonWrongPrefix.

@Test
public void valueWithJsonWrongPrefix() throws Exception {
    String jsonPrefix = "prefix";
    StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
    assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new JsonPathResultMatchers("$.str").prefix("wrong").value("foo").match(result));
}
Also used : StubMvcResult(org.springframework.test.web.servlet.StubMvcResult) Test(org.junit.jupiter.api.Test)

Example 4 with StubMvcResult

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

the class JsonPathResultMatchersTests method valueWithJsonPrefixNotConfigured.

@Test
public void valueWithJsonPrefixNotConfigured() throws Exception {
    String jsonPrefix = "prefix";
    StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
    assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new JsonPathResultMatchers("$.str").value("foo").match(result));
}
Also used : StubMvcResult(org.springframework.test.web.servlet.StubMvcResult) Test(org.junit.jupiter.api.Test)

Example 5 with StubMvcResult

use of org.springframework.test.web.servlet.StubMvcResult 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.jupiter.api.Test)

Aggregations

StubMvcResult (org.springframework.test.web.servlet.StubMvcResult)17 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)12 Test (org.junit.jupiter.api.Test)8 HttpStatus (org.springframework.http.HttpStatus)2 MvcResult (org.springframework.test.web.servlet.MvcResult)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)1 FlashMap (org.springframework.web.servlet.FlashMap)1