Search code examples
testingmockingwiremock

Is it possible in Wiremock to save tests in JSON with fixed, custom name


I write tests in Wiremock in Java and use the WireMock.saveAllMappings() command to save test in JSON.

    @Test
    public void hello() {
        stubFor(get(urlEqualTo("/hello")).willReturn(aResponse()
                        .withStatus(200)
                        .withBody("Hello!")
        ));

        WireMock.saveAllMappings();
    }

The problem is that every time I run the test, it is saved again with a different new name. Is it possible to specify a fixed name or save it only one time ?


Solution

  • You can call .withName("your stub name") and .withId(aUUID) on the stub building DSL, which will fix these values and should avoid this issue.

    Alternatively you can set .persistent(true) which will cause the stub to be saved at the point it's created.