/
RestClientResponseExceptionTest.java
62 lines (51 loc) · 2.31 KB
/
RestClientResponseExceptionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.hellokoding.springcore;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withBadRequest;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withServerError;
public class RestClientResponseExceptionTest {
private static final String URL = "/server/products/1";
private MockRestServiceServer mockRestServiceServer;
private RestTemplate restTemplate;
@BeforeEach
public void setUp() {
restTemplate = new RestTemplate();
mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);
}
@Test
public void response4xx() {
mockRestServiceServer
.expect(requestTo(URL))
.andExpect(method(HttpMethod.GET))
.andRespond(withBadRequest());
ResponseEntity responseEntity = consumeWebService(URL, Object.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
}
@Test
public void response5xx() {
mockRestServiceServer
.expect(requestTo(URL))
.andExpect(method(HttpMethod.GET))
.andRespond(withServerError());
ResponseEntity responseEntity = consumeWebService(URL, Object.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
<T> ResponseEntity consumeWebService(String url, Class<T> responseType) {
try {
return restTemplate.getForEntity(url, responseType);
} catch (RestClientResponseException e) {
return ResponseEntity
.status(e.getRawStatusCode())
.body(e.getResponseBodyAsString());
}
}
}