I am using Wiremock (spring-cloud-contract-wiremock) in my @SpringBootTest so:
import static com.github.tomakehurst.wiremock.client.WireMock.*;
@SpringBootTest
@AutoConfigureWireMock(port = 0)
class SomeTest {
@Test
void test1() {
Wiremock.stubFor(get(urlPathEqualTo("/endpoint"))
.willReturn(aResponse().withStatus(200))
);
// start test
// assertThat response is 200
}
@Test
void test2() {
stubFor(get(urlPathEqualTo("/endpoint"))
.willReturn(aResponse().withStatus(401))
);
// start test
// assertThat response is 401
}
}
Thus, for two test methods I make different stubs for the same request and it works well.
Also, when using Wiremock, you can store stubs in JSON format in the src/test/resources/mappings package, but so far I can only achieve such a result that I can only have one JSON file in which I store the stubs.
How can I have two separate JSON for two test methods and how can I tell my test method to use a specific JSON file to load Wiremock-stubs?
I am using spring-boot-parent 3.1.0 and this maven dependency for WireMock:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-wiremock</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>
Format of json must be following: