Hello there... I have some trouble understanding t...
# help
r
Hello there... I have some trouble understanding the WireMock integration in Spring Boot. Setting up a stub seems to be possible by either using static
WireMock.stubFor()
or WireMockExtension's
wireMockExtension.stubFor()
. In the following
@SpringBootTest
only
wireMockExtension.stubFor()
seems to work.
WireMock.stubFor()
gives a 404 and a message that there are no registered stubs. What's the difference between both variants and in which cases would one use one or the other? Here is a sample test...
Copy code
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@WireMockTest
class WiremockSpringbootApplicationTests {

    @Value("${remote.url}")
    private String remoteUrl;

    @RegisterExtension
    static WireMockExtension wireMockExtension = WireMockExtension.newInstance()
            .options(wireMockConfig().dynamicPort())
            .build();

    // set remote.url dynamically based on WireMock instance
    @DynamicPropertySource
    static void dynamicProperties(DynamicPropertyRegistry registry) {
        registry.add("remote.url", wireMockExtension::baseUrl);
    }

    @Test
    void should_return_test() {
        final WebClient webClient = WebClient.builder().baseUrl(remoteUrl).build();

        // this stubbing works...
        wireMockExtension.stubFor(get("/test")
                .willReturn(aResponse().withBody("test")));

        // ... this doesn't and gives a 404
        // WireMock.stubFor(get("/test")
        //         .willReturn(aResponse().withBody("test")));

        final String responseString = webClient.get().uri("/test")
                .retrieve()
                .bodyToMono(String.class)
                .block();

        Assertions.assertThat(responseString).isEqualTo("test");
    }
}
s
Hi @Robert Strauch I am facing similar kind of issue with stubFor, thus trying with wireMockExtensions. Can you please confirm in remoteUrl, what value you have kept? Thanks