Hi Team I'm trying wiremock in a SpringBoot applic...
# help
k
Hi Team I'm trying wiremock in a SpringBoot application, however while booting the application I'm getting the below error
Failed to instantiate [com.github.tomakehurst.wiremock.WireMockServer]: Factory method 'wireMockServerConfig' threw exception with message: STRIP_TRAILING_BIGDECIMAL_ZEROES
Code Snippet:
Copy code
@SpringBootApplication
@ConfigurationPropertiesScan
public class Application {
    public static void main(String[] args) {



        SpringApplication.run(Application.class, args);
    }


    @Bean
    public WireMockServer wireMockServerConfig() {
        return new WireMockServer(WireMockConfiguration.wireMockConfig().notifier(new ConsoleNotifier(true))
                .port(8082));
    }
}
Can you please help me to solve this?
l
Hi, can you let us know what version of spring you are using and what WireMock dependency you have added. Was there any stacktrace along with the error message? Also, it looks like you are creating the WireMock server as part of your main Spring application context. Are you planning on using this in a test or for some other reason ?
k
Copy code
'springBootVersion', '3.1.8'
implementation 'org.wiremock:wiremock:3.5.4'
So, I'm trying to create an Spring Boot application, where the stubs which are written as Java code gets reflected in
GET /__admin/mappings
on application boot up in similar manner like the JSON stubs which are kept in the this path
src/test/resources/mappings
I'm able to solve that issue now my application is booting. However, the wiremock server is not getting started. On calling http://localhost:8082/__admin/mappings I can see that the server is not started. Code Snippet:
Copy code
@SpringBootApplication
@ConfigurationPropertiesScan
public class Application {
    public static void main(String[] args) {

        System.setProperty("spring.cloud.compatibility-verifier.enabled", "false");

        SpringApplication.run(Application.class, args);
    }


    @Bean
    public WireMockServer wireMockServerConfig() {
        return new WireMockServer(WireMockConfiguration.wireMockConfig().notifier(new ConsoleNotifier(true))
                .port(8082));
    }
}
Stubbing:
Copy code
@Component
public class Stubbings {

    @Autowired
    public WireMockServer wireMockServer;


    public StubMapping stubTest () {
        return wireMockServer.stubFor(post(urlPathEqualTo("/test/call")) // Define a POST request stub
                .willReturn(ok()) // Return a 200 OK response
                .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "application/json") // Set response headers
                        .withBody("{ \"message\": \"Success\", \"transactionId\": \"12345\" }"))
                .withServeEventListener(
                        "webhook",
                        webhook()
                                .withMethod(POST)
                                .withUrl("<http://localhost:8080>")
                                .withHeader("Content-Type", "application/json")
                                .withBody("{ \"message\": \"success\", \"transactionId\": \"{{jsonPath originalRequest.body '$.transactionId'}}\" }") // Webhook body with JSON path
                ));
    }
}
And other than this there are couple of json mocks in this path
src/test/resources/mappings
l
Not sure if you have seen but we have released a jetty 12 version of wiremock that can be used with Spring Boot 3 - https://github.com/wiremock/wiremock-examples/blob/main/spring-boot/spring-boot-3/wiremock-jetty-12/build.gradle
I am still a little confused as to what you are trying to achieve. If you want a server up and running that you can interact with then I would run WireMock in standalone mode as per this web page - https://wiremock.org/docs/standalone/java-jar/
k
Hi Lee thanks for your support now the Application is up and Running it also picking all the mocking both from json files and java classes. This is the working code snippet.
Copy code
@SpringBootApplication
@ConfigurationPropertiesScan
@ComponentScan({"packageName"})
public class Application {

    private final List<StubInterface> stubs;

    public Application(List<StubInterface> stubs) {
        this.stubs = stubs;
    }

    public static void main(String[] args) {
        System.setProperty("spring.cloud.compatibility-verifier.enabled", "false");
        SpringApplication.run(Application.class, args);
    }

    @EventListener(ApplicationReadyEvent.class)
    public void mockServer(){

        var wm = new WireMockServer(WireMockConfiguration.wireMockConfig().port(8082));
        wm.start();

        stubs.forEach(stub -> stub.stubTest(wm));
    }

}