|
| 1 | +package com.baeldung.wiremock; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.okJson; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Assertions; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 12 | +import org.springframework.http.HttpStatus; |
| 13 | +import org.springframework.http.ResponseEntity; |
| 14 | +import org.wiremock.spring.ConfigureWireMock; |
| 15 | +import org.wiremock.spring.EnableWireMock; |
| 16 | +import org.wiremock.spring.InjectWireMock; |
| 17 | + |
| 18 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 19 | + |
| 20 | +@SpringBootTest(classes = SimpleWiremockIntegrationTest.AppConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 21 | +@EnableWireMock({ |
| 22 | + @ConfigureWireMock(name = "user-service", port = 8081), |
| 23 | + @ConfigureWireMock(name = "product-service", port = 8082) |
| 24 | +}) |
| 25 | +public class InjectedWiremockIntegrationTest { |
| 26 | + |
| 27 | + @InjectWireMock("user-service") |
| 28 | + WireMockServer mockUserService; |
| 29 | + |
| 30 | + @InjectWireMock("product-service") |
| 31 | + WireMockServer mockProductService; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private TestRestTemplate restTemplate; |
| 35 | + |
| 36 | + @Test |
| 37 | + void givenEmptyUserList_whenFetchingUsers_thenReturnsEmptyList() { |
| 38 | + mockUserService.stubFor(get("/users").willReturn(okJson("[]"))); |
| 39 | + |
| 40 | + ResponseEntity<String> response = restTemplate |
| 41 | + .getForEntity("http://localhost:8081/users", String.class); |
| 42 | + |
| 43 | + Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 44 | + Assertions.assertEquals("[]", response.getBody()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void givenUserAndProductLists_whenFetchingUsersAndProducts_thenReturnsMockedData() { |
| 49 | + mockUserService |
| 50 | + .stubFor(get("/users") |
| 51 | + .willReturn(okJson("[{\"id\": 1, \"name\": \"John\"}]"))); |
| 52 | + mockProductService |
| 53 | + .stubFor(get("/products") |
| 54 | + .willReturn(okJson("[{\"id\": 101, \"name\": \"Laptop\"}]"))); |
| 55 | + |
| 56 | + ResponseEntity<String> userResponse = restTemplate |
| 57 | + .getForEntity("http://localhost:8081/users", String.class); |
| 58 | + ResponseEntity<String> productResponse = restTemplate |
| 59 | + .getForEntity("http://localhost:8082/products", String.class); |
| 60 | + |
| 61 | + Assertions.assertEquals(HttpStatus.OK, userResponse.getStatusCode()); |
| 62 | + Assertions.assertEquals("[{\"id\": 1, \"name\": \"John\"}]", userResponse.getBody()); |
| 63 | + |
| 64 | + Assertions.assertEquals(HttpStatus.OK, productResponse.getStatusCode()); |
| 65 | + Assertions.assertEquals("[{\"id\": 101, \"name\": \"Laptop\"}]", productResponse.getBody()); |
| 66 | + } |
| 67 | + |
| 68 | + @SpringBootApplication |
| 69 | + static class AppConfiguration {} |
| 70 | +} |
0 commit comments