Skip to content

ekino/wiremock-unused-stubs-junit

Repository files navigation

Wiremock unused stubs junit extension

This extension will help you to track unnecessary wiremock stubs defined in your tests. It supports junit4 and junit5 engine.

Build Status GitHub (pre-)release Maven Central

Dependencies

Maven

Junit 5 :

<dependency>
    <groupId>com.ekino.oss.wiremock</groupId>
    <artifactId>wiremock-unused-stubs-junit-jupiter</artifactId>
    <version>1.1.1</version>
    <scope>test</scope>
</dependency>

Junit 4 :

<dependency>
    <groupId>com.ekino.oss.wiremock</groupId>
    <artifactId>wiremock-unused-stubs-junit-vintage</artifactId>
    <version>1.1.1</version>
    <scope>test</scope>
</dependency>

Gradle

Junit 5 :

testImplementation("com.ekino.oss.wiremock:wiremock-unused-stubs-junit-jupiter:1.1.1")

Junit 4 :

testImplementation("com.ekino.oss.wiremock:wiremock-unused-stubs-junit-vintage:1.1.1")

How to use it ?

For use this library you will need to register your extension using junit. You will need to give two information to the extensions :

  • A list of the wiremock server you want to monitor
  • If you want or not activate the silent mode. It will display a warning message when unnecessary stubs are found.

Junit 5

import com.ekino.oss.wiremock.WireMockStubExtension
import com.github.tomakehurst.wiremock.WireMockServer
import org.junit.jupiter.api.extension.RegisterExtension

public class SampleClassTest {
    private WireMockServer wireMockServer = WireMockServer(1234);
    
    @RegisterExtension
    private static WireMockStubExtension wireMockStubExtension = WireMockStubExtension(listOf(wireMockServer), silent = true);
}

Junit 4

import com.ekino.oss.wiremock.WireMockJunit4Extension
import com.github.tomakehurst.wiremock.WireMockServer
import org.junit.ClassRule

public class SampleClassTest {
    private WireMockServer wireMockServer = WireMockServer(1234);
    
    @ClassRule
    private static WireMockJunit4Extension wireMockStubExtension = WireMockJunit4Extension(listOf(wireMockServer), silent = true);
}

Junit 5 Wiremock Extension

This extension now the support, the junit 5 extension provided by wiremock.

You can use the extension with the static DSL :

@WireMockTest(httpPort = 1234)
@ExtendWith(WireMockStubExtension::class)
public class ClassTest {

    // failed test
    @Test
    void test1() {
        stubFor(
                get(WireMock.urlPathEqualTo("/some-url"))
                        .willReturn(WireMock.ok())
        );
    }

    // Success test
    @Test
    void test2() {
        stubFor(
                get(WireMock.urlPathEqualTo("/some-url"))
                        .willReturn(WireMock.ok())
        );

        HttpUriRequest request = new HttpGet("http://localhost:1234/some-url");
        HttpClientBuilder.create().build().execute(request);
    }
}

You can use the extension by injecting WireMockRuntimeInfo in test parameter :

@WireMockTest(httpPort = 1234)
@ExtendWith(WireMockStubExtension::class)
class ClassTest {

    // failed test
    @Test
    void test1(WireMockRuntimeInfo wmRuntimeInfo) {
        WireMock wireMock = wmRuntimeInfo.getWireMock();

        wireMock.register(
            get(WireMock.urlPathEqualTo("/some-url"))
                .willReturn(WireMock.ok())
        );
    }

    // success test
    @Test
    void test2(WireMockRuntimeInfo wmRuntimeInfo) {
        WireMock wireMock = wmRuntimeInfo.getWireMock();

        wireMock.register(
            get(WireMock.urlPathEqualTo("/some-url"))
                .willReturn(WireMock.ok())
        );

        HttpUriRequest request = new HttpGet("http://localhost:1234/some-url");
        HttpClientBuilder.create().build().execute(request);
    }
}

You can still use the programmatic way to define junit 5 wiremock extension like this :

class ClassTest {
    
    @RegisterExtension 
    private static WireMockExtension wiremockExtension = WireMockExtension.newInstance()
        .options(wireMockConfig().port(1234))
        .build();

    @RegisterExtension 
    private static  WireMockStubExtension wireMockStubExtension = WireMockStubExtension(wireMockRuntimeInfos = listOf(wiremockExtension));

    // failed test
    @Test
    void test1() {
        wiremockExtension.stubFor(
            WireMock.get(WireMock.urlPathEqualTo("/some-url"))
                .willReturn(WireMock.ok())
        );
    }

    // success test
    @Test
    void test2() {
        wiremockExtension.stubFor(
            WireMock.get(WireMock.urlPathEqualTo("/some-url"))
                .willReturn(WireMock.ok())
        );

        HttpUriRequest request = new HttpGet("http://localhost:1234/some-url");
        HttpClientBuilder.create().build().execute(request);
    }
}