|
@@ -0,0 +1,70 @@
|
|
1
|
+package com.yaozhitech.spring5;
|
|
2
|
+
|
|
3
|
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
|
4
|
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
|
5
|
+import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
|
6
|
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
|
7
|
+import static org.assertj.core.api.Assertions.assertThat;
|
|
8
|
+
|
|
9
|
+import org.junit.Before;
|
|
10
|
+import org.junit.Test;
|
|
11
|
+import org.junit.runner.RunWith;
|
|
12
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
13
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
14
|
+import org.springframework.boot.web.server.LocalServerPort;
|
|
15
|
+import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
|
|
16
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
17
|
+import org.springframework.test.web.reactive.server.WebTestClient;
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+@RunWith(SpringRunner.class)
|
|
21
|
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
|
22
|
+ properties = {"httpbin=http://localhost:${wiremock.server.port}"})
|
|
23
|
+@AutoConfigureWireMock(port = 0)
|
|
24
|
+public class ApplicationTest {
|
|
25
|
+
|
|
26
|
+ @Autowired
|
|
27
|
+ private WebTestClient webClient;
|
|
28
|
+
|
|
29
|
+ @LocalServerPort
|
|
30
|
+ private int port;
|
|
31
|
+
|
|
32
|
+ @Before
|
|
33
|
+ public void setUp() throws Exception {
|
|
34
|
+ System.out.println(String.format("port is : [%d]", port));
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ @Test
|
|
38
|
+ public void contextLoads() throws Exception {
|
|
39
|
+ //Stubs
|
|
40
|
+ stubFor(get(urlEqualTo("/get"))
|
|
41
|
+ .willReturn(aResponse()
|
|
42
|
+ .withBody("{\"headers\":{\"Hello\":\"World\"}}")
|
|
43
|
+ .withHeader("Content-Type", "application/json")));
|
|
44
|
+ stubFor(get(urlEqualTo("/delay/3"))
|
|
45
|
+ .willReturn(aResponse()
|
|
46
|
+ .withBody("no fallback")
|
|
47
|
+ .withFixedDelay(3000)));
|
|
48
|
+
|
|
49
|
+ stubFor(get(urlEqualTo("/city_list.do"))
|
|
50
|
+ .willReturn(aResponse()
|
|
51
|
+ .withBody("{}")
|
|
52
|
+ .withFixedDelay(100)));
|
|
53
|
+
|
|
54
|
+// webClient
|
|
55
|
+// .get().uri("/get")
|
|
56
|
+// .exchange()
|
|
57
|
+// .expectStatus().isOk()
|
|
58
|
+// .expectBody()
|
|
59
|
+// .jsonPath("$.headers.Hello").isEqualTo("World");
|
|
60
|
+
|
|
61
|
+ webClient
|
|
62
|
+ .get().uri("/city_list")
|
|
63
|
+// .header("Host", "www.hystrix.com")
|
|
64
|
+ .exchange()
|
|
65
|
+ .expectStatus().isOk()
|
|
66
|
+ .expectBody()
|
|
67
|
+ .consumeWith(
|
|
68
|
+ response -> assertThat(response.getResponseBody()).isEqualTo("{}".getBytes()));
|
|
69
|
+ }
|
|
70
|
+}
|