org.springframework.test.web.servlet.StubMvcResult Java Examples

The following examples show how to use org.springframework.test.web.servlet.StubMvcResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonPathResultMatchersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test(expected = AssertionError.class)
public void prefixWithPayloadNotLongEnough() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json");
	response.getWriter().print(new String("test".getBytes("ISO-8859-1")));
	StubMvcResult result =  new StubMvcResult(null, null, null, null, null, null, response);

	new JsonPathResultMatchers("$.str").prefix("prefix").value("foo").match(result);
}
 
Example #2
Source File: PrintingResultHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void printHandlerNull() throws Exception {
	StubMvcResult mvcResult = new StubMvcResult(this.request, null, null, null, null, null, this.response);
	this.handler.handle(mvcResult);

	assertValue("Handler", "Type", null);
}
 
Example #3
Source File: PrintingResultHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void printHandlerNull() throws Exception {
	StubMvcResult mvcResult = new StubMvcResult(this.request, null, null, null, null, null, this.response);
	this.handler.handle(mvcResult);

	assertValue("Handler", "Type", null);
}
 
Example #4
Source File: XpathResultMatchersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void stringEncodingDetection() throws Exception {
	String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<person><name>Jürgen</name></person>";
	byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	StreamUtils.copy(bytes, response.getOutputStream());
	StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response);

	new XpathResultMatchers("/person/name", null).string("Jürgen").match(result);
}
 
Example #5
Source File: StatusResultMatchersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void statusRanges() throws Exception {
	for (HttpStatus status : HttpStatus.values()) {
		MockHttpServletResponse response = new MockHttpServletResponse();
		response.setStatus(status.value());
		MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
		switch (status.series().value()) {
			case 1:
				this.matchers.is1xxInformational().match(mvcResult);
				break;
			case 2:
				this.matchers.is2xxSuccessful().match(mvcResult);
				break;
			case 3:
				this.matchers.is3xxRedirection().match(mvcResult);
				break;
			case 4:
				this.matchers.is4xxClientError().match(mvcResult);
				break;
			case 5:
				this.matchers.is5xxServerError().match(mvcResult);
				break;
			default:
				fail("Unexpected range for status code value " + status);
		}
	}
}
 
Example #6
Source File: StatusResultMatchersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
	}
}
 
Example #7
Source File: XpathResultMatchersTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void stringEncodingDetection() throws Exception {
	String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<person><name>Jürgen</name></person>";
	byte[] bytes = content.getBytes(Charset.forName("UTF-8"));
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	StreamUtils.copy(bytes, response.getOutputStream());
	StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response);

	new XpathResultMatchers("/person/name", null).string("Jürgen").match(result);
}
 
Example #8
Source File: JsonPathResultMatchersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test(expected = AssertionError.class)
public void prefixWithPayloadNotLongEnough() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json");
	response.getWriter().print(new String("test".getBytes("ISO-8859-1")));
	StubMvcResult result =  new StubMvcResult(null, null, null, null, null, null, response);

	new JsonPathResultMatchers("$.str").prefix("prefix").value("foo").match(result);
}
 
Example #9
Source File: PrintingResultHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void printHandlerNull() throws Exception {
	StubMvcResult mvcResult = new StubMvcResult(this.request, null, null, null, null, null, this.response);
	this.handler.handle(mvcResult);

	assertValue("Handler", "Type", null);
}
 
Example #10
Source File: StatusResultMatchersTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpStatusCodeResultMatchers() throws Exception {

	List<AssertionError> failures = new ArrayList<AssertionError>();

	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);
	}
}
 
Example #11
Source File: StatusResultMatchersTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void statusRanges() throws Exception {

	for(HttpStatus status : HttpStatus.values()) {

		MockHttpServletResponse response = new MockHttpServletResponse();
		response.setStatus(status.value());
		MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
		switch (status.series().value()) {
			case 1:
				this.matchers.is1xxInformational().match(mvcResult);
				break;
			case 2:
				this.matchers.is2xxSuccessful().match(mvcResult);
				break;
			case 3:
				this.matchers.is3xxRedirection().match(mvcResult);
				break;
			case 4:
				this.matchers.is4xxClientError().match(mvcResult);
				break;
			case 5:
				this.matchers.is5xxServerError().match(mvcResult);
				break;
			default:
				fail("Unexpected range for status code value " + status);
		}
	}
}
 
Example #12
Source File: StatusResultMatchersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void statusRanges() throws Exception {
	for (HttpStatus status : HttpStatus.values()) {
		MockHttpServletResponse response = new MockHttpServletResponse();
		response.setStatus(status.value());
		MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
		switch (status.series().value()) {
			case 1:
				this.matchers.is1xxInformational().match(mvcResult);
				break;
			case 2:
				this.matchers.is2xxSuccessful().match(mvcResult);
				break;
			case 3:
				this.matchers.is3xxRedirection().match(mvcResult);
				break;
			case 4:
				this.matchers.is4xxClientError().match(mvcResult);
				break;
			case 5:
				this.matchers.is5xxServerError().match(mvcResult);
				break;
			default:
				fail("Unexpected range for status code value " + status);
		}
	}
}
 
Example #13
Source File: XpathResultMatchersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void stringEncodingDetection() throws Exception {
	String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<person><name>Jürgen</name></person>";
	byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	StreamUtils.copy(bytes, response.getOutputStream());
	StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response);

	new XpathResultMatchers("/person/name", null).string("Jürgen").match(result);
}
 
Example #14
Source File: StatusResultMatchersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
	}
}
 
Example #15
Source File: MockMvcResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.sendRedirect(redirectUrl);
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
	return mvcResult;
}
 
Example #16
Source File: FlashAttributeResultMatchersTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private StubMvcResult getStubMvcResult() {
	FlashMap flashMap = new FlashMap();
	flashMap.put("good", "good");
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, flashMap, null);
	return mvcResult;
}
 
Example #17
Source File: MockMvcResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private StubMvcResult getForwardedUrlStubMvcResult(String forwardedUrl) {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.setForwardedUrl(forwardedUrl);
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
	return mvcResult;
}
 
Example #18
Source File: ContentResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json; charset=UTF-8");
	response.getWriter().print(new String(CONTENT.getBytes("UTF-8")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example #19
Source File: ModelResultMatchersTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private MvcResult getMvcResult(ModelAndView modelAndView) {
	return new StubMvcResult(null, null, null, null, modelAndView, null, null);
}
 
Example #20
Source File: JsonPathResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test(expected = AssertionError.class)
public void valueWithJsonPrefixNotConfigured() throws Exception {
	String jsonPrefix = "prefix";
	StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
	new JsonPathResultMatchers("$.str").value("foo").match(result);
}
 
Example #21
Source File: XpathResultMatchersTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	response.getWriter().print(new String(RESPONSE_CONTENT.getBytes("ISO-8859-1")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example #22
Source File: MockMvcResultMatchersTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.sendRedirect(redirectUrl);
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
	return mvcResult;
}
 
Example #23
Source File: MockMvcResultMatchersTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private StubMvcResult getForwardedUrlStubMvcResult(String forwardedUrl) {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.setForwardedUrl(forwardedUrl);
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
	return mvcResult;
}
 
Example #24
Source File: ContentResultMatchersTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json; charset=UTF-8");
	response.getWriter().print(new String(CONTENT.getBytes("UTF-8")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example #25
Source File: JsonPathResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private StubMvcResult createPrefixedStubMvcResult(String jsonPrefix) throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json");
	response.getWriter().print(jsonPrefix + new String(RESPONSE_CONTENT.getBytes("ISO-8859-1")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example #26
Source File: JsonPathResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void valueWithJsonPrefix() throws Exception {
	String jsonPrefix = "prefix";
	StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
	new JsonPathResultMatchers("$.str").prefix(jsonPrefix).value("foo").match(result);
}
 
Example #27
Source File: JsonPathResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test(expected = AssertionError.class)
public void valueWithJsonWrongPrefix() throws Exception {
	String jsonPrefix = "prefix";
	StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
	new JsonPathResultMatchers("$.str").prefix("wrong").value("foo").match(result);
}
 
Example #28
Source File: XpathResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	response.getWriter().print(new String(RESPONSE_CONTENT.getBytes(StandardCharsets.ISO_8859_1)));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example #29
Source File: FlashAttributeResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private StubMvcResult getStubMvcResult() {
	FlashMap flashMap = new FlashMap();
	flashMap.put("good", "good");
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, flashMap, null);
	return mvcResult;
}
 
Example #30
Source File: ModelResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private MvcResult getMvcResult(ModelAndView modelAndView) {
	return new StubMvcResult(null, null, null, null, modelAndView, null, null);
}