|
| 1 | +package app; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 5 | +import static org.assertj.core.api.Assertions.assertThat; |
| 6 | + |
| 7 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 8 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 9 | +import io.restassured.RestAssured; |
| 10 | +import org.junit.jupiter.api.DisplayName; |
| 11 | +import org.junit.jupiter.api.Nested; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest; |
| 16 | +import org.springframework.core.env.Environment; |
| 17 | +import org.wiremock.spring.ConfigureWireMock; |
| 18 | +import org.wiremock.spring.EnableWireMock; |
| 19 | +import org.wiremock.spring.InjectWireMock; |
| 20 | + |
| 21 | +@SpringBootTest(classes = NestedClassSingleWireMockTest.AppConfiguration.class) |
| 22 | +@EnableWireMock({ |
| 23 | + @ConfigureWireMock( |
| 24 | + name = "todo-service", |
| 25 | + baseUrlProperties = "todo-service.url", |
| 26 | + portProperties = "todo-service.port") |
| 27 | +}) |
| 28 | +public class NestedClassSingleWireMockTest { |
| 29 | + |
| 30 | + @SpringBootApplication |
| 31 | + static class AppConfiguration {} |
| 32 | + |
| 33 | + @Autowired private Environment environment; |
| 34 | + |
| 35 | + @InjectWireMock("todo-service") |
| 36 | + private WireMockServer topLevelClassTodoService; |
| 37 | + |
| 38 | + @Nested |
| 39 | + @DisplayName("Test Something") |
| 40 | + class NestedTest { |
| 41 | + |
| 42 | + @InjectWireMock("todo-service") |
| 43 | + private WireMockServer nestedClassTodoService; |
| 44 | + |
| 45 | + @Test |
| 46 | + void injectsWiremockServerToNestedClassField() { |
| 47 | + this.assertWireMockServer( |
| 48 | + this.nestedClassTodoService, "todo-service.url", "todo-service.port"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void injectsWiremockServerToTopLevelClassField() { |
| 53 | + this.assertWireMockServer( |
| 54 | + NestedClassSingleWireMockTest.this.topLevelClassTodoService, |
| 55 | + "todo-service.url", |
| 56 | + "todo-service.port"); |
| 57 | + } |
| 58 | + |
| 59 | + private void assertWireMockServer( |
| 60 | + final WireMockServer wireMockServer, final String property, final String portProperty) { |
| 61 | + assertThat(wireMockServer).as("creates WireMock instance").isNotNull(); |
| 62 | + assertThat(wireMockServer.baseUrl()).as("WireMock baseUrl is set").isNotNull(); |
| 63 | + assertThat(wireMockServer.port()).as("sets random port").isNotZero(); |
| 64 | + assertThat( |
| 65 | + Integer.valueOf( |
| 66 | + NestedClassSingleWireMockTest.this.environment.getProperty(portProperty))) |
| 67 | + .as("sets Spring port property") |
| 68 | + .isEqualTo(wireMockServer.port()); |
| 69 | + assertThat(NestedClassSingleWireMockTest.this.environment.getProperty(property)) |
| 70 | + .as("sets Spring property") |
| 71 | + .isEqualTo(wireMockServer.baseUrl()); |
| 72 | + |
| 73 | + // Test that WireMock is configured for the correct WireMock instance |
| 74 | + // Suffixed with port to make it differ for different test runs and servers |
| 75 | + final String mockedPath = "/the_default_prop_mock-" + wireMockServer.port(); |
| 76 | + WireMock.stubFor(get(mockedPath).willReturn(aResponse().withStatus(202))); |
| 77 | + RestAssured.baseURI = wireMockServer.baseUrl(); |
| 78 | + RestAssured.when().get(mockedPath).then().statusCode(202); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments