Tal Kaptsan
06/25/2024, 11:35 AM@WebMvcTest(controllers = HerokuResource.class)
@AutoConfigureMockMvc
@WireMockTest(httpPort = 8200)
@ActiveProfiles("test")
@Slf4j
@TestPropertySource(properties = {
"sm.url=<http://localhost:8200>",
"sm.hostname=localhost",
"sm.basic.auth.username=user",
"sm.basic.auth.password=12345",
"statemachine.retries=2",
"statemachine.waitms=3600"
})
class WireMockTest extends ITBase {
All tests uses same stubs. I have a function which I call from a @BeforeEachRafe Arnold
06/25/2024, 12:01 PMTal Kaptsan
06/25/2024, 12:09 PM@BeforeEach
public void setUp() throws InterruptedException {
jedis = jedisPool.getResource();
redisCleanup(jedis, uuid);
setupStubs();
}
private void setupStubs() {
// Subscription
stubFor(WireMock.get(WireMock.urlPathEqualTo("/subscription"))
.willReturn(aResponse()
.withStatus(204)
.withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.withHeader("Accept", MediaType.APPLICATION_JSON_VALUE)
.withHeader("Authorization", authorization_redis)
.withBody(""))); // No subscriptions
stubFor(<http://WireMock.post|WireMock.post>(WireMock.urlPathMatching("/subscription.*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.withHeader("Accept", MediaType.APPLICATION_JSON_VALUE)
.withBody("{\"subscription_id\": \"4444\", \"account_id\": \"2222\", \"status\": \"active\"}")));
// many more others
}
Rafe Arnold
06/25/2024, 12:49 PMWireMock.reset()
should do the jobTal Kaptsan
06/25/2024, 12:52 PMRafe Arnold
06/25/2024, 12:59 PMTal Kaptsan
06/25/2024, 1:05 PMvalidateNoErrorResponse
private CloseableHttpResponse makeHttpPost(String url, String body) throws IOException {
HttpPost postRequest = initHttpPost(url);
postRequest.setEntity(new StringEntity(body));
CloseableHttpResponse response = httpClient.execute(postRequest);
validateNoErrorResponse(response);
return response;
}
Rafe Arnold
06/25/2024, 1:26 PM