Программисты JAVA общаются здесь
Anonymous
Wiremock AutoConfigureWireMock httphostConnectException: соединение отказано
Сообщение
Anonymous » 07 окт 2025, 12:59
Код: Выделить всё
@ActiveProfiles("it")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = ServiceApplication.class)
@AutoConfigureWireMock(port = 0)
@TestPropertySource(properties = {
"server.servlet.context-path=http://localhost:${wiremock.server.port}/abc"
})
public class ABCTest {
@Autowired
protected TestRestTemplate restTemplate;
private static final String INIT_URL = "/init";
private static final String JSON_RESPONSE = """
{"result":...
"generatedAt":"2025-10-06T06:46:28.745Z"}
""";
@BeforeClass
public void beforeClass() {
}
@Test
public void init() throws IOException {
stubFor(get(urlEqualTo("/abc" + INIT_URL))
.willReturn(aResponse()
.withStatus(status)
.withHeader("Content-Type", "application/json")
.withBody(JSON_RESPONSE)));
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.add("X-API-KEY", apiKey);
HttpEntity entity = new HttpEntity("body", headers);
ResponseEntity response = restTemplate.exchange(INIT_URL, HttpMethod.GET, entity, String.class);
System.out.println(response.getBody());
}
< /code>
Я получил это: < /p>
wiremock.org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0: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:682)
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
at java.base/java.net.Socket.connect(Socket.java:751)
at wiremock.org.apache.hc.client5.http.socket.PlainConnectionSocketFactory$1.run(PlainConnectionSocketFactory.java:87)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:571)
Я использовал Spring-cloud-contract-wiremock-4.3.0.jar и Spring-boot-test-3.5.4.jar
Подробнее здесь:
https://stackoverflow.com/questions/797 ... on-refused
1759831151
Anonymous
[code]@ActiveProfiles("it") @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(classes = ServiceApplication.class) @AutoConfigureWireMock(port = 0) @TestPropertySource(properties = { "server.servlet.context-path=http://localhost:${wiremock.server.port}/abc" }) public class ABCTest { @Autowired protected TestRestTemplate restTemplate; private static final String INIT_URL = "/init"; private static final String JSON_RESPONSE = """ {"result":... "generatedAt":"2025-10-06T06:46:28.745Z"} """; @BeforeClass public void beforeClass() { } @Test public void init() throws IOException { stubFor(get(urlEqualTo("/abc" + INIT_URL)) .willReturn(aResponse() .withStatus(status) .withHeader("Content-Type", "application/json") .withBody(JSON_RESPONSE))); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.add("X-API-KEY", apiKey); HttpEntity entity = new HttpEntity("body", headers); ResponseEntity response = restTemplate.exchange(INIT_URL, HttpMethod.GET, entity, String.class); System.out.println(response.getBody()); } < /code> Я получил это: < /p> wiremock.org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0: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:682) at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549) at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592) at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) at java.base/java.net.Socket.connect(Socket.java:751) at wiremock.org.apache.hc.client5.http.socket.PlainConnectionSocketFactory$1.run(PlainConnectionSocketFactory.java:87) at java.base/java.security.AccessController.doPrivileged(AccessController.java:571) [/code] Я использовал Spring-cloud-contract-wiremock-4.3.0.jar и Spring-boot-test-3.5.4.jar Подробнее здесь: [url]https://stackoverflow.com/questions/79783467/wiremock-autoconfigurewiremock-httphostconnectexception-connection-refused[/url]