I'm using WireMock to test a Spring Boot web app that, itself uses Jetty, though on different ports.
What I'm seeing is that, if I use the basic Rule configuration:
@Rule
public WireMockRule wireMockRule = new WireMockRule(8006);
and I use stubFor
in a @Before
method, and then immediately follow that up with a call to the stubbed URL (via Spring's RestTemplate
), the first test case works without a hitch, but then the second test case hangs. The bit of relevant stack trace is:
"main" prio=10 tid=0x00007f60a4009000 nid=0xb8d0 runnable [0x00007f60abcec000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:146)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
- locked <0x00000000f33e8d00> (a java.io.BufferedInputStream)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:688)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:767)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1162)
- locked <0x00000000f33da7c8> (a sun.net.www.protocol.http.HttpURLConnection)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:397)
at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:48)
at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:33)
at org.springframework.web.client.DefaultResponseErrorHandler.getHttpStatusCode(DefaultResponseErrorHandler.java:56)
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:50)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:542)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:502)
at org.springframework.web.client.RestTemplate.put(RestTemplate.java:381)
If I instead use the static methods shown on the 'Getting Started' page, everything works fine. I'm fine with going with the static method so that WireMock is started/stopped once per class, but for my own edification I would love to know why the other case isn't working as expected.
UPDATE: Just noticed that after ~200 seconds, the test fails with:
nested exception is java.net.SocketException: Unexpected end of file from server
Thanks, Justin
In my project I use random port for WireMock @Rule WireMockRule wireMockRule = new WireMockRule(Options.DYNAMIC_PORT)
And it is working perfect. It's better solution because after change you can run tests in parallel:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With