ApplicationTest.java 2.4 KB

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