javaspring-bootwiremock

How to get wiremock running before the spring boot application status up?


I have an integration test for a spring boot micro-service. The problem is that the service calls an external service (via REST) on startup. I’m using WireMock to mock the call. Spring makes the application start before WireMock is started. Because of this, the rest call fails and so does the service.

The call is made by a library that is also made by our company, so I cannot change anything there.

Do you have any suggestions for me?


Solution

  • you might create static instance of WireMockServer in your test. Here is a code sample:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class YourApplicationTests {
        static WireMockServer mockHttpServer = new WireMockServer(10000); // endpoint port here
    
        @BeforeClass
        public static void setup() throws Exception {
            mockHttpServer.stubFor(get(urlPathMatching("/")).willReturn(aResponse().withBody("test").withStatus(200)));
            mockHttpServer.start();
        }
    
        @AfterClass
        public static void teardown() throws Exception {
            mockHttpServer.stop();
        }
    
        @Test
        public void someTest() throws Exception {
            // your test code here
        }
    }