My unit tests use wiremock extensively and most of these test classes look like this:
class Test {
private static WireMockServer wireMockServer;
@BeforeAll
static void setup() {
wireMockServer = new WireMockServer(wireMockConfig().port(8080));
wireMockServer.start();
}
@AfterAll
static void teardown() {
wireMockServer.stop();
}
@AfterEach
void deleteScenariosAndRequests() {
resetAllScenarios();
resetAllRequests();
}
@Test
void test1() throws {
stubFor(post(urlEqualTo(SOME_URL))
.willReturn(okJson("{}")));
stubFor(post(urlEqualTo(SOME_OTHER_URL))
.willReturn(okJson("{}")));
stubFor(post(urlEqualTo(EVEN_ANOTHER_URL))
.willReturn(okJson("{}")));
//some action
//some assertions
}
@Test
void test2() {
stubFor(post(urlEqualTo(SOME_URL))
.willReturn(aResponse().withStatus(400)));
stubFor(post(urlEqualTo(SOME_URL))
.willReturn(aResponse().withStatus(400)));
stubFor(post(urlEqualTo(SOME_URL))
.willReturn(aResponse().withStatus(400)));
//some action
//some assertions
}
}
So as you can see what I basically do is to define in each test the stubs that I actually need for this test.
Is this actual good pratice? I see myself repeating the same stubs again and again. On the other hand, there is the advantage that each test explicitly states what it needs.
Is there any commonly agreed best practice how to manage wiremock stubs in java unit tests?
Aucun commentaire:
Enregistrer un commentaire