Search code examples
micronautwiremock

starting wiremock as part of local development


I have a hello world micronaut application which starts locally and makes some calls to an external service. What I want to do is start wiremock as part of the local build on my machine, so that wiremock can intercept the external calls and send responses

Im not sure how to do this in micronaut, in spring id create a "dev" config that would start wiremock... but not sure how that works in micronaut..

any help?


Solution

  • Have you tried instructions provided in https://github.com/tomakehurst/wiremock-jdk8-examples/ ?

    Add dependency:

    test "com.github.tomakehurst:wiremock-jre8-standalone:2.33.2"

    and than use it from your code e.g.:

    import com.github.tomakehurst.wiremock.junit.WireMockRule;
    import com.github.tomakehurst.wiremock.matching.MatchResult;
    import org.junit.Rule;
    import org.junit.Test;
    
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
    import static com.github.tomakehurst.wiremock.client.WireMock.requestMadeFor;
    import static com.github.tomakehurst.wiremock.client.WireMock.requestMatching;
    import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.Matchers.is;
    
    public class LambdaRequestMatcherTest {
    
        @Rule
        public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort());
    
        @Test
        public void lambdaStubMatcher() throws Exception {
            wm.stubFor(requestMatching(
                    request -> MatchResult.of(request.getUrl().contains("magic"))
                ).willReturn(aResponse()));
    
            URL url = new URL("http://localhost:" + wm.port() + "/the-magic-path");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
            assertThat(connection.getResponseCode(), is(200));
        }
    
        @Test
        public void lambdaVerification() {
            wm.verify(requestMadeFor(request ->
                MatchResult.of(!request.header("My-Header").values().isEmpty()))
            );
        }
    
    }