Last active
October 21, 2022 20:16
-
-
Save wenqiglantz/4780e026e61ca55ee7676797b3beb14d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | |
| @ExtendWith(MockitoExtension.class) | |
| @ContextConfiguration(initializers = {WireMockInitializer.class}) | |
| class CustomerServiceClientIT { | |
| @Autowired | |
| private WebTestClient client; | |
| @Autowired | |
| private WireMockServer wireMockServer; | |
| public static String CUSTOMER_SUCCESSFUL_RESPONSE = | |
| """ | |
| { | |
| "customerId": "123", | |
| "firstName": "John", | |
| "lastName": "Smith" | |
| } | |
| """; | |
| public static String UPDATE_CUSTOMER_SUCCESSFUL_RESPONSE = | |
| """ | |
| { | |
| "customerId": "123", | |
| "firstName": "Jane", | |
| "lastName": "Smith" | |
| } | |
| """; | |
| @AfterEach | |
| public void afterEach() { | |
| this.wireMockServer.resetAll(); | |
| } | |
| @Test | |
| void testCreateCustomer() throws Exception { | |
| wireMockServer.stubFor(post(urlPathMatching("/customers")) | |
| .willReturn(aResponse() | |
| .withStatus(200) | |
| .withHeader("Content-type", | |
| "application/json") | |
| .withBody(CUSTOMER_SUCCESSFUL_RESPONSE))); | |
| CustomerVO request = CustomerVO.builder() | |
| .firstName("John") | |
| .lastName("Smith") | |
| .build(); | |
| client.post().uri("/customers").body(Mono.just(request), CustomerVO.class) | |
| .exchange().expectStatus().is2xxSuccessful() | |
| .expectBody() | |
| .jsonPath("$.firstName") | |
| .isEqualTo("John") | |
| .jsonPath("$.lastName") | |
| .isEqualTo("Smith") | |
| .jsonPath("$.customerId") | |
| .isNotEmpty(); | |
| } | |
| @Test | |
| void testGetCustomer() throws Exception { | |
| wireMockServer.stubFor(get(urlPathMatching("/customers/([a-zA-Z0-9]*)")) | |
| .willReturn(aResponse() | |
| .withStatus(200) | |
| .withHeader("Content-type", | |
| "application/json") | |
| .withBody(CUSTOMER_SUCCESSFUL_RESPONSE))); | |
| client.get().uri("/customers/123") | |
| .exchange().expectStatus().is2xxSuccessful() | |
| .expectBody() | |
| .jsonPath("$.firstName") | |
| .isEqualTo("John") | |
| .jsonPath("$.lastName") | |
| .isEqualTo("Smith") | |
| .jsonPath("$.customerId") | |
| .isNotEmpty(); | |
| } | |
| @Test | |
| void testUpdateCustomer() throws Exception { | |
| wireMockServer.stubFor(put(urlPathMatching("/customers/([a-zA-Z0-9]*)")) | |
| .willReturn(aResponse() | |
| .withStatus(200) | |
| .withHeader("Content-type", | |
| "application/json") | |
| .withBody(UPDATE_CUSTOMER_SUCCESSFUL_RESPONSE))); | |
| CustomerVO request = CustomerVO.builder() | |
| .firstName("Jane") | |
| .lastName("Smith") | |
| .build(); | |
| client.put().uri("/customers/123").body(Mono.just(request), CustomerVO.class) | |
| .exchange().expectStatus().is2xxSuccessful() | |
| .expectBody() | |
| .jsonPath("$.firstName") | |
| .isEqualTo("Jane") | |
| .jsonPath("$.lastName") | |
| .isEqualTo("Smith") | |
| .jsonPath("$.customerId") | |
| .isNotEmpty(); | |
| } | |
| @Test | |
| void testDeleteCustomer() throws Exception { | |
| wireMockServer.stubFor(delete(urlPathMatching("/customers/([a-zA-Z0-9]*)")) | |
| .willReturn(aResponse() | |
| .withStatus(200))); | |
| client.delete().uri("/customers/123") | |
| .exchange().expectStatus().is2xxSuccessful() | |
| .expectBody() | |
| .isEmpty(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment