Vadivel Murugan
11/30/2023, 9:29 AM@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebClient(registerRestTemplate = true)
class BookApiServiceITest {
@RegisterExtension
static WireMockExtension wireMockServer =
WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort().usingFilesUnderClasspath("wiremock"))
.build();
@Autowired private BookApiService bookApiService;
@test
void testCreateEntitlement() {
stubFor(
post(urlEqualTo("/entitlement"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.withHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.withBody("{\"result\": \"success\"}")));
BookEntitlementResponse entitlement =
bookApiService.createEntitlement(bookObject, "product");
}
}
When I run the test getting below exception, Do we need to set any static port & host to run the testcases?
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:542)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597)
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.lambda$connectSocket$0(PlainConnectionSocketFactory.java:85)
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.socket.ConnectionSocketFactory.connectSocket(ConnectionSocketFactory.java:113)
at wiremock.org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:181)
at wiremock.org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:447)
at wiremock.org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:162)
at wiremock.org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:172)
at wiremock.org.apache.hc.client5.http.impl.classic.ConnectExec.execute(ConnectExec.java:142)
at wiremock.org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
at wiremock.org.apache.hc.client5.http.impl.classic.ProtocolExec.execute(ProtocolExec.java:192)
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:123)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:524)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:507)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:483)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:150)
at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:446)
at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:441)
at com.github.tomakehurst.wiremock.client.WireMock.givenThat(WireMock.java:130)
at com.github.tomakehurst.wiremock.client.WireMock.stubFor(WireMock.java:134)
Tom
11/30/2023, 9:58 AM.configureStaticDsl(true)
to the config builder