https://linen.dev logo
#help
Title
# help
g

Gee

06/20/2023, 10:29 AM
Hello, I am using testcontainers with wiremock, but I am getting the error: connect to localhost:8080 failed connection refused no further information. Please suggest some solution to this.
o

Oleg Nenashev

06/20/2023, 10:32 AM
Could you please share the code?
Also note that a random port is set by default in WireMock Testcontainers. You should use the
wiremockServer.getUrl(String relative)
method for the majority of operations in your test clients
g

Gee

06/20/2023, 10:45 AM
Hello @Oleg Nenashev thanks for responding Here is the sample code from the poc that I am doing
Copy code
import com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
public class WireMockContainerPOC {

    @Container
    public GenericContainer<?> wireMockContainer = new GenericContainer<>(DockerImageName.parse("rodolpheche/wiremock:2.27.2"))
            .withExposedPorts(8080);

    @Test
    public void testStubbedAPI() {
        String wireMockBaseUrl = "http://" + wireMockContainer.getHost() + ":" + wireMockContainer.getMappedPort(8080);

        

        WireMock.stubFor(
                WireMock.get(WireMock.urlEqualTo("/api/endpoint"))
                        .willReturn(WireMock.aResponse()
                                .withStatus(200)
                                .withHeader("Content-Type", "application/json")
                                .withBody("{\"message\": \"Stubbed Response\"}"))
        );

        // Make a request to the stubbed API
        String response = wireMockContainer.getHost() + ":" + wireMockContainer.getMappedPort(8080) + "/api/endpoint";

        // Assertions
        assertThat(response).isNotNull();
        assertThat(response).isEqualTo("{\"message\": \"Stubbed Response\"}");


    }
}
o

Oleg Nenashev

06/20/2023, 10:50 AM
I would need to debug it but I don't see any issue immediately. At the same time, why don't you use https://github.com/wiremock/wiremock-testcontainers-java ?
Regarding your code, I suppose the docker image fails to start, maybe because of missing some configuration. You use an old version of WireMock and the image, so I would advise upgrading before spending too much time on that
g

Gee

06/20/2023, 10:58 AM
oh okay, I faced the same issue with wiremock 2.35.0 as well. I had tried to use https://github.com/wiremock/wiremock-testcontainers-java as well, but I am getting the same error
o

Oleg Nenashev

06/20/2023, 10:59 AM
Do you see anything in the container startup log?
g

Gee

06/20/2023, 11:03 AM
No Oleg
It exits as soon as it starts running
o

Oleg Nenashev

06/20/2023, 11:06 AM
Did you try that environment manually? It is unlikely to exit without no logs at all
I mean manual
docker run
g

Gee

06/20/2023, 11:08 AM
so, when I do docker run. followed by the image name, I can see that the port is 8080 and the rest is all false
@Oleg Nenashev please advice me on this
@Oleg Nenashev Another error that I get while running this https://github.com/wiremock/wiremock-testcontainers-java is
Copy code
Resource WireMockContainerTest/hello-world.json relative to org.wiremock.integrations.testcontainers.WireMockContainerTest not found
I had placed the json mapping exactly like in the repo