https://linen.dev logo
#wiremock-java
Title
# wiremock-java
a

Alex Arana

09/21/2023, 1:54 AM
After recently migrating my Kotlin application to Spring Boot 3, I modified the way I use WireMock stubs within my integration and functional tests from running in standalone mode via
spring-cloud-contract-wiremock:4.0.4
to running a WireMock server within a Docker container using
wiremock-testcontainers-java:1.0-alpha-7
:
Copy code
private val WIREMOCK_IMAGE = DockerImageName.parse("wiremock/wiremock") // use 'latest' tag, currently 3.0.0-1
private val wireMockContainer = WireMockContainer(WIREMOCK_IMAGE)
    .withCliArg("--global-response-templating")
    .withFileSystemBind("src/test/resources/stubs", "/home/wiremock", BindMode.READ_ONLY)
    .withExposedPorts(8080)
    .waitingFor(
        Wait.forLogMessage(".*port:\\s*8080.*", 1)
            .withStartupTimeout(25.seconds.toJavaDuration())
    )
However, I am unable to run the HttpAdminClient against the external WireMock server even after reconfiguring to use the port mapped on the local host:
Copy code
with(wireMockContainer) {
        start()
        val wireMockServerPort = getMappedPort(8080)
        logger.debug { "WireMock container baseUrl: '$baseUrl'" }

        WireMock.configureFor(wireMockServerPort)
        val result = WireMock.listAllStubMappings()
        <http://logger.info|logger.info> { "Loaded ${result.mappings.size} WireMock stubs to Docker image.." }
    }
Attempting to list all stub mappings in the above initialisation code triggers the following error:
Copy code
org.apache.hc.client5.http.HttpHostConnectException: Connect to <http://localhost:32769> [localhost/127.0.0.1] failed: Connection refused
    at java.base@17.0.8.1/sun.nio.ch.Net.pollConnect(Native Method)
    at java.base@17.0.8.1/sun.nio.ch.Net.pollConnectNow(Net.java:672)
    at java.base@17.0.8.1/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:547)
    at java.base@17.0.8.1/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:602)
    at java.base@17.0.8.1/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base@17.0.8.1/java.net.Socket.connect(Socket.java:633)
    at app//org.apache.hc.client5.http.socket.PlainConnectionSocketFactory.lambda$connectSocket$0(PlainConnectionSocketFactory.java:85)
    at java.base@17.0.8.1/java.security.AccessController.doPrivileged(AccessController.java:569)
    at app//org.apache.hc.client5.http.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:84)
    at app//org.apache.hc.client5.http.socket.ConnectionSocketFactory.connectSocket(ConnectionSocketFactory.java:113)
    at app//org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:181)
    at app//org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:447)
    at app//org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:162)
    at app//org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:172)
    at app//org.apache.hc.client5.http.impl.classic.ConnectExec.execute(ConnectExec.java:142)
    at app//org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
    at app//org.apache.hc.client5.http.impl.classic.ProtocolExec.execute(ProtocolExec.java:192)
    at app//org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
    at app//org.apache.hc.client5.http.impl.classic.InternalHttpClient.doExecute(InternalHttpClient.java:170)
    at app//org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:123)
    at app//com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:524)
    at app//com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:507)
    at app//com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:483)
    at app//com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:473)
    at app//com.github.tomakehurst.wiremock.client.HttpAdminClient.listAllStubMappings(HttpAdminClient.java:180)
    at app//com.github.tomakehurst.wiremock.client.WireMock.allStubMappings(WireMock.java:462)
    at app//com.github.tomakehurst.wiremock.client.WireMock.listAllStubMappings(WireMock.java:150)
I can only replicate this issue in my CI/CD environment (Azure DevOps) but running locally appears to work.. Has anyone come across this issue?
o

Oleg Nenashev

09/21/2023, 6:26 AM
Hello. Could you please try with the
3.1.0-1
Docker image? You also override the
waitingFor
method, and hence the embedded health check does not get tried. I would assume the container fails to start though
a

Alex Arana

09/22/2023, 7:00 AM
I did try Docker image
3.1.0-1
and removed
waitingFor
override. Result failed just like it did before so I enabled container logging (preceded with WIREMOCK) and I can see that the container started successfully and received the request to list all mappings:
Copy code
2023-09-22T06:50:17.792Z  INFO 368 --- [    Test worker] o.t.c.wait.strategy.HttpWaitStrategy     : /eager_kowalevski: Waiting for 60 seconds for URL: <http://172.17.0.1:32769/__admin/mappings> (where port 32769 maps to container port 8080)
2023-09-22T06:50:18.136Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: 2023-09-22 06:50:18.131 Verbose logging enabled
2023-09-22T06:50:18.677Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: 2023-09-22 06:50:18.677 Verbose logging enabled
2023-09-22T06:50:19.000Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: 
2023-09-22T06:50:19.001Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: The WireMock server is started .....
2023-09-22T06:50:19.003Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: port:                         8080
2023-09-22T06:50:19.004Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: enable-browser-proxying:      false
2023-09-22T06:50:19.005Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: disable-banner:               true
2023-09-22T06:50:19.006Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: no-request-journal:           false
2023-09-22T06:50:19.006Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: verbose:                      true
2023-09-22T06:50:19.008Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: 
2023-09-22T06:50:19.927Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: 2023-09-22 06:50:19.925 Admin request received:
2023-09-22T06:50:19.928Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: 172.17.0.1 - GET /mappings
2023-09-22T06:50:19.929Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: 
2023-09-22T06:50:19.930Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: User-Agent: [Java/17.0.8.1]
2023-09-22T06:50:19.930Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: Host: [172.17.0.1:32769]
2023-09-22T06:50:19.931Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: Accept: [text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2]
2023-09-22T06:50:19.931Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: Connection: [keep-alive]
2023-09-22T06:50:19.932Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: 
2023-09-22T06:50:19.932Z  INFO 368 --- [eam--1949476450] .BaseServiceIntegrationTest : WIREMOCK: 
2023-09-22T06:50:20.038Z  INFO 368 --- [    Test worker] tc.wiremock/wiremock:3.1.0-1             : Container wiremock/wiremock:3.1.0-1 started in PT12.37483945S
2023-09-22T06:50:20.040Z DEBUG 368 --- [    Test worker] .BaseServiceIntegrationTest : WireMock container baseUrl: '<http://172.17.0.1:32769>'
2023-09-22T06:50:20.583Z ERROR 368 --- [    Test worker] o.s.boot.SpringApplication               : Application run failed

    org.apache.hc.client5.http.HttpHostConnectException: Connect to <http://localhost:32769> [localhost/127.0.0.1] failed: Connection refused
Problem fixed. I needed to point HttpAdminClient to the bridging IP and not "localhost" when running Docker-in-Docker.
🙌 1
o

Oleg Nenashev

09/28/2023, 7:24 AM
Good to know, thanks
4 Views