Hi Tom, hi WireMock Users,
thanks a lot for your prompt request. You're right, I probably want to use the scenario feature to differentiate between different states on the server. While digging deeper, I also found a different solution. I can also load a subset of the total set of json mappings per test case. So on the client side the user is able to read in only the json stubs required for his expected scenario.
This lead me to building a spring-boot-starter for WireMock, which now becomes another shameless plug for the users of this list ;-) (At the moment also hosted in the same repository, see
https://github.com/ePages-de/restdocs-wiremock)
Here is an example of using WireMock using the starter:
@RunWith(SpringJUnit4ClassRunner.class) // (1)
@SpringApplicationConfiguration(classes = { ClientApplication.class }) // (2)
@ActiveProfiles("test") // (3)
@WireMockTest(stubPath = "wiremock/restdocs-server") // (4)
public class NoteServiceTest {
@Autowired
private WireMockServer wireMockServer; // (5)
...
}- Use Spring's JUnit Runner (as of 1.4.0 this will be called
SpringRunner), for this is an integration test. - Include the usual application configuration classes
- Extend your test with properties to point to your WireMock server.
In the example in our github repo, we are using a Spring Expression inside
application-test.properties to point our service to
WireMock by setting: service.baseUri=http://localhost:${wiremock.port}/ - The
@WireMockTest annotation enables the wireMockServer bean, which can be accessed
from your test's application context. By default, it starts a WireMockServer on a dynamic port, but you could also set it to a fixed port. The stubPath property can be used to point to a classpath resource folder that holds your json stubs. - If you want, you can auto-wire the
WireMockServer instance, and re-configure it, just as described in the official WireMock documentation.
As mentioned above, it is possible to read-in a subset of mappings for each test, by repeating the `@WireMockTest` annotation on the test method.
The stubPath is concatenated from the values given on the test class and the test method, just as a `@RequestMapping` annotation in Spring would.
In the example given below, the resulting stubPath provided to WireMock is composed as `wiremock/myservice/specific-mappings`.
@WireMockTest(stubPath = "wiremock/myservice")
public class MyTest {
@Test
@WireMockTest(stubPath = "specific-mappings")
public void testDifferentMappings() {
....
}Cheers & big thanks for the good work! Always happy for your feedback!
Oliver