Fixed the Spring Boot App that is running commercially. There is a place in the App that uses RestTemplate to call the REST API of other linked systems, and unit tests including that part were carried out by setting up a mock server with WireMock.
When I added a test according to the renovation, the existing test suddenly
NoHttpResponseException:[server] failed to respond
I started to moss with exceptions.
It seems that it is not a mistake of the test itself because it is in a state where it passes when the test is executed alone and it is moss when it is executed through. So the investigation started.
It seems to be a bug that occurs when HttpURLConnection is used for the Http Client of the app that accesses WireMock.
https://github.com/tomakehurst/wiremock/issues/97 This is applicable because Rest Template of Spring also uses HttpURLConnection internally.
Use spring-cloud-contract-wiremock.
https://github.com/tomakehurst/wiremock/issues/97 The issue shows several types of countermeasures, so it seems that you should adopt the one you like. The selection criteria are as follows.
Increase Spring Boot version or change HttpClient Since it is a commercial application, configuration changes are basically NG for testing → ✕
Disable connection reuse for HttpClient I don't know where to set it, and it may affect the operation of the app → ✕
Use of spring-cloud-contract-wire mock Completed by changing the test library → ○
As the name suggests, spring-cloud-contract-wiremock uses wireMock internally, so changes to the test code were minimal. Specifically, just change the notation of the server settings and specify the directory of the existing test resource. See Official for detailed usage.
Configuration(before)
public class ExampleUnitTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule(8080);
....
Configuration(after)
@AutoConfigureWireMock(port = 8080, stubs = "classpath*:/META-INF/**/mappings/**/*.json")
public class ExampleUnitTest {
....
As mentioned above, in my case, the existing test code can be passed as it is just by changing the path of the test resource specified by stubs =
.
Recommended Posts