Java Code Examples for org.springframework.test.web.client.response.DefaultResponseCreator

Following code examples demonstrate how to use org.springframework.test.web.client.response.DefaultResponseCreatorfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.test.web.client.response.DefaultResponseCreatorand various code implementation of this class.

	public void status_WithCodeAndReason_ReturnsCorrectStatusCodeAndReasonAndStatusResponseBody() throws Exception {
		HttpStatus httpStatus = SEE_OTHER;
		String reason = "OK";
		
		URI expectedUri = uriBuilder().path("/status/{code}/{reason}").buildAndExpand(httpStatus.value(), reason).toUri();
		
		StatusResponse statusResponse = new StatusResponse(httpStatus.value(), reason);
		
		HttpHeaders headers = new HttpHeaders();
		headers.add(CONTENT_TYPE, APPLICATION_JSON_VALUE);
		
		ResponseEntity<StatusResponse> expectedResponseEntity = 
				new ResponseEntity<StatusResponse>(statusResponse, headers, OK);
		
		DefaultResponseCreator response = 
				withStatus(OK)
					.body("{\"code\": 303, \"message\": \"OK\"}")
					.contentType(APPLICATION_JSON);
		
		mockServer.expect(requestTo(expectedUri))
			.andExpect(method(GET))
			.andRespond(response);

		ResponseEntity<StatusResponse> responseEntity = mockbin.statusOperations().customStatus(httpStatus, reason);
		
		assertThat(responseEntity, equalTo(expectedResponseEntity));
	} 


	public void status_WithCode_ReturnsCorrectStatusCodeAndReasonAndStatusResponseBody() throws Exception {
		HttpStatus httpStatus = SEE_OTHER;
		
		URI expectedUri = uriBuilder().path("/status/{code}/See Other").buildAndExpand(httpStatus.value()).toUri();
		
		StatusResponse statusResponse = new StatusResponse(httpStatus.value(), httpStatus.getReasonPhrase());
		
		HttpHeaders headers = new HttpHeaders();
		headers.add(CONTENT_TYPE, APPLICATION_JSON_VALUE);
		
		ResponseEntity<StatusResponse> expectedResponseEntity = 
				new ResponseEntity<StatusResponse>(statusResponse, headers, SEE_OTHER);
		
		DefaultResponseCreator response = 
				withStatus(SEE_OTHER)
					.body("{\"code\": 303, \"message\": \"See Other\"}")
					.contentType(APPLICATION_JSON);
		
		mockServer.expect(requestTo(expectedUri))
			.andExpect(method(GET))
			.andRespond(response);

		ResponseEntity<StatusResponse> responseEntity = 
				mockbin.statusOperations().customStatus(httpStatus);
		
		assertThat(responseEntity, equalTo(expectedResponseEntity));
	} 

    public MockRestServiceServer mockRestServiceServer() throws Exception {
        MockRestServiceServer mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).build(new NoResetRequestExpectationManager());

        byte[] responseBody = readAllBytes(get("src", "test", "resources", "mock", "response", "remote.server.request.products.findAll.200.json"));
        DefaultResponseCreator responseCreator = withStatus(HttpStatus.OK).body(responseBody).contentType(MediaType.APPLICATION_JSON_UTF8);
        mockRestServiceServer.expect(min(1), requestTo(productsURI)).andExpect(method(HttpMethod.GET)).andRespond(responseCreator);

        responseBody = readAllBytes(get("src", "test", "resources", "mock", "response", "remote.server.response.products.findOne.1.200.json"));
        responseCreator = withStatus(HttpStatus.OK).body(responseBody).contentType(MediaType.APPLICATION_JSON_UTF8);
        mockRestServiceServer.expect(min(1), requestTo(productsURI + "/1")).andExpect(method(HttpMethod.GET)).andRespond(responseCreator);

        responseBody = readAllBytes(get("src", "test", "resources", "mock", "response", "remote.server.response.products.findOne.2.200.json"));
        responseCreator = withStatus(HttpStatus.OK).body(responseBody).contentType(MediaType.APPLICATION_JSON_UTF8);
        mockRestServiceServer.expect(min(1), requestTo(productsURI + "/2")).andExpect(method(HttpMethod.GET)).andRespond(responseCreator);

        return mockRestServiceServer;
    } 

	public static void mockService(RestTemplate template, DefaultResponseCreator response) {
		MockRestServiceServer mockServer = MockRestServiceServer.createServer(template);		 		 
		 					 .andExpect(method(HttpMethod.PUT))
		 					 .andRespond(response);
	} 

	public void withStatusForbidden() throws Exception {
		DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(HttpStatus.FORBIDDEN);
		MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

		assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
		assertTrue(response.getHeaders().isEmpty());
	} 

	public void serverError() throws Exception {
		DefaultResponseCreator responseCreator = MockRestResponseCreators.withServerError();
		MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

		assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
		assertTrue(response.getHeaders().isEmpty());
	} 

	public void withStatusUnAuthorized() throws Exception {
		DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(HttpStatus.UNAUTHORIZED);
		MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

		assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
		assertTrue(response.getHeaders().isEmpty());
	} 

Advertisement
Javadoc
A ResponseCreator with builder-style methods for adding response details. @author Rossen Stoyanchev

@since 3.2

Read More
Advertisement