Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 752
Yacine Ait Yaiz Avatar asked Jan 02 '18 18:01

Yacine Ait Yaiz


People also ask

What is the use of WireMock in spring boot?

WireMock is a library for stubbing and mocking web services. It constructs an HTTP server that acts as an actual web service. In simple terms, it is a mock server that is highly configurable and has the ability to return recorded HTTP responses for requests matching criteria.

How do I check my application status in spring boot?

For running the Spring Boot application, open the main application file, and run it as Java Application. When the application runs successfully, it shows the message in the console, as shown below. Now, open the browser and invoke the URL http://localhost:8080.

What is default port for WireMock?

Note: When you specify this parameter, WireMock will still, additionally, bind to an HTTP port (8080 by default). So when running multiple WireMock servers you will also need to specify the --port parameter in order to avoid conflicts.


1 Answers

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
    }
}
like image 87
Alex M981 Avatar answered Oct 12 '22 09:10

Alex M981