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?
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.
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.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With