1

I am writing integration test to mock external local service called dates-service-svc which actually runs on port 8081 . I am using wireMock to mock the service and HTTP client to issue a request to dates-services-svc , but the requests issued by HTTP client doesn't hit the wireMock . and raises an "connection refused " exception . when the test case finish its execution . the logs says

Requests received: [ ]

On the other hand ,I issued the request using the post man . wireMock replies with that The requested URL matches the stub registered

I configured the wireMock as following

@Rule
    public WireMockRule wireMockServer = new WireMockRule(new WireMockConfiguration().port(8084)
            .notifier(new ConsoleNotifier(true)));
@Before
    public void setUp() {
        wireMockServer.start();
        WireMock.configureFor("localhost", 8084);
    }
    @AfterEach
    public void afterEach() {
        wireMockServer.resetAll();
    }

    @After
    public void tearUp() {
        wireMockServer.stop();
    }

I created very simple test cases to just test whether WireMock works fine or not .here it is

 @Test
    public  void testWireMock()
    {
        try {

            configureFor("localhost", 8084);
            stubFor(any(urlEqualTo("/v1/service1/local-dates/"))
                    .willReturn(aResponse().withStatus(200).withBody("hello wireMOck")));;
         
            String datesEndPoint = "https://dates-service-svc/v1/service1/local-dates/";
      HttpClient client = HttpClient.newBuilder()
                    .version(HttpClient.Version.HTTP_1_1)
                    .followRedirects(HttpClient.Redirect.NORMAL)
                    .connectTimeout(Duration.ofSeconds(20))
                    .build();
            URI uri = new URIBuilder(paymentDatesEndPoint)     
                    .build();
            HttpRequest request = HttpRequest.newBuilder()
                    .GET()
                    .uri(uri)
                    .build();
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            List<ServeEvent> allServeEvents = getAllServeEvents();
            List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/v1/.*")));
            verify(getRequestedFor(urlEqualTo("/v1/service1/local-dates/")));
        }
        catch (Exception e)
        {
            Assertions.fail(e);
//            System.out.println(e.getMessage());
        }
    }

logs

021-10-07 14:01:57.710 Verbose logging enabled 2021-10-07 14:01:57.861 INFO 7407 --- [ main] wiremock.org.eclipse.jetty.util.log
: Logging initialized @22479ms to wiremock.org.eclipse.jetty.util.log.Slf4jLog 2021-10-07 14:01:58.034 INFO 7407 --- [ main] w.org.eclipse.jetty.server.Server
: jetty-9.4.30.v20200611; built: 2020-06-11T12:34:51.929Z; git: 271836e4c1f4612f12b7bb13ef5a92a927634b0d; jvm 11.0.12+8-LTS-237 2021-10-07 14:01:58.050 INFO 7407 --- [ main] w.o.e.j.server.handler.ContextHandler : Started w.o.e.j.s.ServletContextHandler@47e2a158{/__admin,null,AVAILABLE} 2021-10-07 14:01:58.057 INFO 7407 --- [ main] w.o.e.j.server.handler.ContextHandler : Started w.o.e.j.s.ServletContextHandler@65dce54c{/,null,AVAILABLE} 2021-10-07 14:01:58.076 INFO 7407 --- [ main] w.o.e.jetty.server.AbstractConnector : Started NetworkTrafficServerConnector@61186191{HTTP/1.1, (http/1.1)}{0.0.0.0:8084} 2021-10-07 14:01:58.076 INFO 7407 --- [
main] w.org.eclipse.jetty.server.Server : Started @22695ms 2021-10-07 14:01:58.875 INFO 7407 --- [p1096402541-143] w.o.e.j.s.h.ContextHandler.__admin : RequestHandlerClass from context returned com.github.tomakehurst.wiremock.http.AdminRequestHandler. Normalized mapped under returned 'null' 2021-10-07 14:01:58.897 Admin request received: 127.0.0.1 - POST /mappings

Connection: [keep-alive] User-Agent: [Apache-HttpClient/4.5.12 (Java/11.0.12)] Host: [localhost:8084] Content-Length: [264] Content-Type: [text/plain; charset=UTF-8] { "id" : "e149540c-33bf-4b39-8d56-5653fb77cdd9", "request" : { "url" : "/v1/service1/local-dates/", "method" : "ANY" }, "response" : { "status" : 200, "body" : "hello wireMOck" }, "uuid" : "e149540c-33bf-4b39-8d56-5653fb77cdd9" }

2021-10-07 14:01:59.141 INFO 7407 --- [ main] w.o.e.jetty.server.AbstractConnector : Stopped NetworkTrafficServerConnector@61186191{HTTP/1.1, (http/1.1)}{0.0.0.0:8084} 2021-10-07 14:01:59.142 INFO 7407 --- [
main] w.o.e.j.server.handler.ContextHandler : Stopped w.o.e.j.s.ServletContextHandler@65dce54c{/,null,UNAVAILABLE} 2021-10-07 14:01:59.143 INFO 7407 --- [ main] w.o.e.j.server.handler.ContextHandler : Stopped w.o.e.j.s.ServletContextHandler@47e2a158{/__admin,null,UNAVAILABLE}

org.opentest4j.AssertionFailedError at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:47) at org.junit.jupiter.api.Assertions.fail(Assertions.java:140) at com.sympl.txn.handler.TxnHandlerControllerIntegrationTests.testWireMock(TxnHandlerControllerIntegrationTests.java:229) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at com.github.tomakehurst.wiremock.junit.WireMockRule$1.evaluate(WireMockRule.java:79) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) Caused by: java.net.ConnectException at java.net.http/jdk.internal.net.http.HttpClientImpl.send(HttpClientImpl.java:561) at java.net.http/jdk.internal.net.http.HttpClientFacade.send(HttpClientFacade.java:119) at com.sympl.txn.handler.TxnHandlerControllerIntegrationTests.testWireMock(TxnHandlerControllerIntegrationTests.java:222) ... 34 more Caused by: java.net.ConnectException at java.net.http/jdk.internal.net.http.common.Utils.toConnectException(Utils.java:971) at java.net.http/jdk.internal.net.http.PlainHttpConnection.connectAsync(PlainHttpConnection.java:179) at java.net.http/jdk.internal.net.http.AsyncSSLConnection.connectAsync(AsyncSSLConnection.java:56) at java.net.http/jdk.internal.net.http.Http1Exchange.sendHeadersAsync(Http1Exchange.java:237) at java.net.http/jdk.internal.net.http.Exchange.lambda$responseAsyncImpl0$8(Exchange.java:435) at java.net.http/jdk.internal.net.http.Exchange.checkFor407(Exchange.java:367) at java.net.http/jdk.internal.net.http.Exchange.lambda$responseAsyncImpl0$9(Exchange.java:439) at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:930) at java.base/java.util.concurrent.CompletableFuture.uniHandleStage(CompletableFuture.java:946) at java.base/java.util.concurrent.CompletableFuture.handle(CompletableFuture.java:2266) at java.net.http/jdk.internal.net.http.Exchange.responseAsyncImpl0(Exchange.java:439) at java.net.http/jdk.internal.net.http.Exchange.responseAsyncImpl(Exchange.java:343) at java.net.http/jdk.internal.net.http.Exchange.responseAsync(Exchange.java:335) at java.net.http/jdk.internal.net.http.MultiExchange.responseAsyncImpl(MultiExchange.java:347) at java.net.http/jdk.internal.net.http.MultiExchange.lambda$responseAsyncImpl$7(MultiExchange.java:384) at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:930) at java.base/java.util.concurrent.CompletableFuture.uniHandleStage(CompletableFuture.java:946) at java.base/java.util.concurrent.CompletableFuture.handle(CompletableFuture.java:2266) at java.net.http/jdk.internal.net.http.MultiExchange.responseAsyncImpl(MultiExchange.java:374) at java.net.http/jdk.internal.net.http.MultiExchange.lambda$responseAsync0$2(MultiExchange.java:293) at java.base/java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:1072) at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506) at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1705) at java.net.http/jdk.internal.net.http.HttpClientImpl$DelegatingExecutor.execute(HttpClientImpl.java:153) at java.base/java.util.concurrent.CompletableFuture.completeAsync(CompletableFuture.java:2591) at java.net.http/jdk.internal.net.http.MultiExchange.responseAsync(MultiExchange.java:246) at java.net.http/jdk.internal.net.http.HttpClientImpl.sendAsync(HttpClientImpl.java:632) at java.net.http/jdk.internal.net.http.HttpClientImpl.send(HttpClientImpl.java:540) ... 36 more Caused by: java.nio.channels.UnresolvedAddressException at java.base/sun.nio.ch.Net.checkAddress(Net.java:130) at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:675) at java.net.http/jdk.internal.net.http.PlainHttpConnection.lambda$connectAsync$0(PlainHttpConnection.java:165) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.net.http/jdk.internal.net.http.PlainHttpConnection.connectAsync(PlainHttpConnection.java:167) ... 62 more

I tried the solutions below:

  • The solution proposed by @P.Raber in this thread
  • I used WireMockServer instead of WireMockRule
  • I removed ConfigureFor() and put it again
  • I configured the wirkmock to start on the same port as the external local service runs (this would sound so dump but I am running out of solutions ) ; however this also made no difference . it still fails
Omar Abo Elsoud
  • 154
  • 2
  • 12
  • Just a guess but your test is reaching out to dates-service-svc and your mock is listening on localhost? – Byron Oct 07 '21 at 13:21
  • yes , I think the mock is listening on the localwhereas the HTTP client is trying to send the request to actual data-service-svc . the request suppose to pass by the WireMock server and returned the mocked response , but this is not case – Omar Abo Elsoud Oct 07 '21 at 17:03
  • @Byron as I mentioned , I issued the request from the post man using *http://localhost:8084//__admin/{requestURL} and I received a reply from WireMock – Omar Abo Elsoud Oct 07 '21 at 17:11

0 Answers0