-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpringFrameworkIssue26720ApplicationTests.java
51 lines (39 loc) · 1.57 KB
/
SpringFrameworkIssue26720ApplicationTests.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
package hu.webhejj.example;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.RequestEntity;
import org.springframework.test.web.client.MockMvcClientHttpRequestFactory;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.client.HttpClientErrorException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
@AutoConfigureMockMvc
class SpringFrameworkIssue26720ApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
void error() {
var restTemplateBuilder = new RestTemplateBuilder()
.requestFactory(() -> new MockMvcClientHttpRequestFactory(mockMvc));
doTest(restTemplateBuilder);
}
@Test
void success() {
var restTemplateBuilder = new RestTemplateBuilder()
.requestFactory(() -> new FixedMockMvcClientHttpRequestFactory(mockMvc));
doTest(restTemplateBuilder);
}
private void doTest(RestTemplateBuilder restTemplateBuilder) {
var restTemplate = restTemplateBuilder.build();
var request = RequestEntity.get("/test").build();
var clientException = assertThrows(HttpClientErrorException.class, () -> {
restTemplate.exchange(request, String.class);
});
var error = clientException.getResponseBodyAsString();
assertEquals("error", error);
}
}