spring-cloud / spring-cloud-contract

Support for Consumer Driven Contracts in Spring
https://cloud.spring.io/spring-cloud-contract
Apache License 2.0
718 stars 439 forks source link

Stubbing in @BeforeAll #2095

Open ebcFlagman opened 7 months ago

ebcFlagman commented 7 months ago

I'm currently upgrading to the latest spring-boot 3.x version and my tests fails now with the following exception:

wiremock.org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:8080 [localhost/127.0.0.1] failed: Connection refused

    at java.base/sun.nio.ch.Net.pollConnect(Native Method)
    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:547)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:602)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at wiremock.org.apache.hc.client5.http.socket.PlainConnectionSocketFactory$1.run(PlainConnectionSocketFactory.java:87)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
    at wiremock.org.apache.hc.client5.http.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:84)
    at wiremock.org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:148)
    at wiremock.org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:396)
    at wiremock.org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:158)
    at wiremock.org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:168)
    at wiremock.org.apache.hc.client5.http.impl.classic.ConnectExec.execute(ConnectExec.java:136)
    at wiremock.org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
    at wiremock.org.apache.hc.client5.http.impl.classic.ExecChainElement$1.proceed(ExecChainElement.java:57)
    at wiremock.org.apache.hc.client5.http.impl.classic.ProtocolExec.execute(ProtocolExec.java:175)
    at wiremock.org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
    at wiremock.org.apache.hc.client5.http.impl.classic.InternalHttpClient.doExecute(InternalHttpClient.java:170)
    at wiremock.org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:75)
    at wiremock.org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:89)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:514)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:497)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:474)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:146)
    at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:418)
    at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:413)
    at com.github.tomakehurst.wiremock.client.WireMock.givenThat(WireMock.java:115)
    at com.github.tomakehurst.wiremock.client.WireMock.stubFor(WireMock.java:119)

My Test is annotated with

@AutoConfigureMockMvc
@AutoConfigureWireMock(port = 0)
@SpringBootTest(
    classes = ApplicationInitializer.class,
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

In the @BeforeAll Method I stub the endpoints.

When I move the stubs into e.g. @BeforeEach everythink works fine.

Used dependency: testImplementation 'org.springframework.cloud:spring-cloud-starter-contract-stub-runner:4.1.1'

When I use in addition the testImplementation 'org.springframework.cloud:spring-cloud-contract-wiremock:4.0.4' the tests run as before the upgrade.

amidani-inovany commented 6 months ago

I have the same issue, 4.0.x works fine 4.1.x generate this error, it's like the ramdom port ${wiremock.server.port} is the default one in the setup method (@BeforeAll)

marcingrzejszczak commented 4 months ago

Please double check that you're using the latest version cause we did fix this problem

ebcFlagman commented 4 months ago

I've update to the latest 4.1.2 version and got the error described in the description

marcingrzejszczak commented 4 months ago

That's not good, we'll look into that ASAP.

cbertoldi commented 1 month ago

@marcingrzejszczak did you have a chance to look into this?