I'm in the process of creating some unit tests for a java service that needs to make HTTP requests to an external web service. I decided to use MockServer (https://www.mock-server.com/#what-is-mockserver) to mock out the external HTTP calls but so far I can't seem to get it to work. I created a dummy test method just to play around with using MockServer. It sets up and starts the server, adds some expectations, makes a simple GET request, and then verifies that the right number of requests were made and that the response has the correct information. The problem is, as soon as my test calls MockServerClient.verify(), the following exception is thrown:
org.mockserver.client.SocketConnectionException: Channel handler removed before valid response has been received
at org.mockserver.client.HttpClientConnectionErrorHandler.handlerRemoved(HttpClientConnectionErrorHandler.java:20)
at io.netty.channel.AbstractChannelHandlerContext.callHandlerRemoved(AbstractChannelHandlerContext.java:946)
at io.netty.channel.DefaultChannelPipeline.callHandlerRemoved0(DefaultChannelPipeline.java:637)
at io.netty.channel.DefaultChannelPipeline.destroyDown(DefaultChannelPipeline.java:876)
at io.netty.channel.DefaultChannelPipeline.destroyUp(DefaultChannelPipeline.java:844)
at io.netty.channel.DefaultChannelPipeline.destroy(DefaultChannelPipeline.java:836)
at io.netty.channel.DefaultChannelPipeline.access$700(DefaultChannelPipeline.java:46)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelUnregistered(DefaultChannelPipeline.java:1392)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelUnregistered(AbstractChannelHandlerContext.java:198)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelUnregistered(AbstractChannelHandlerContext.java:184)
at io.netty.channel.DefaultChannelPipeline.fireChannelUnregistered(DefaultChannelPipeline.java:821)
at io.netty.channel.AbstractChannel$AbstractUnsafe$8.run(AbstractChannel.java:827)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:497)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at java.lang.Thread.run(Thread.java:745)
I have spent the last several days searching for examples and even digging into the source code to try to understand what I'm doing wrong. I'm running out of ideas at this point. If someone could take a look and see where I'm going wrong or at least get me pointed in the right direction I would be grateful.
Here is the code for my unit test:
@Test
public void getShouldWork() throws URISyntaxException, IOException {
ConfigurationProperties.maxSocketTimeout(200000);
final String responseBody = "some_response_body";
final String respHdrName = "some_header";
final String respHdrVal = "some value";
MockServerClient mockServer = ClientAndServer.startClientAndServer(1080);
HttpRequest request = HttpRequest.request()
.withMethod("GET")
.withPath("/some/url")
.withQueryStringParameter("id", "9");
mockServer.when(
request,
Times.exactly(1)
)
.respond(
HttpResponse.response()
.withBody(responseBody)
.withHeader(respHdrName, respHdrVal)
);
//The test will throw an exception when executing this line
mockServer.verify(request, VerificationTimes.exactly(1));
CloseableHttpClient httpClient = HttpClientBuilder.create()
.build();
HttpGet getRequest = new HttpGet("http://localhost:" + mockServer.getPort()
+ "/some/url?id=9");
CloseableHttpResponse resp = httpClient.execute(getRequest);
try (InputStreamReader isr = new InputStreamReader(resp.getEntity().getContent());
BufferedReader br = new BufferedReader(isr);) {
assertThat(br.readLine(), is(responseBody));
while (br.readLine() != null) {
// consume content...
}
}
assertThat(resp.getFirstHeader(respHdrName), is(notNullValue()));
assertThat(resp.getFirstHeader(respHdrName).getValue(), is(respHdrVal));
}