英文:
Wiremock refused to connect while debugging
问题
I have a problem with connecting to my wiremock server that is started from a test. When I put a breakpoint after the wiremock server is started, and I use my browser to go to the specified url, it states: localhost refused to connect.
I have downgraded the test to the most basic version.
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.Test;
public class MyTest {
@Test
public void test() {
WireMockServer server = new WireMockServer(options().bindAddress("127.0.0.1").port(6001));
server.stubFor(get(urlEqualTo("/file"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/plain")
.withBody("Hello world")));
server.start();
assertTrue(server.isRunning());
System.out.println("I put a breakpoint on this line");
}
}
I have tried different options with @WireMockTest as well (and tried about every tutorial I could find), with the same result.
When I inspect the WireMockServer object in debug mode, it shows: server.httpServer.jettyServer = Server@429064e8{STARTED}[9.4.49.v20220914]
So at least it looks like the server is started. Any ideas?
Btw: When I run wiremock standalone on the same port, it can be accessed just fine.
英文:
I have a problem with connecting to my wiremock server that is started from a test. When I put a breakpoint after the wiremock server is started, and I use my browser to go to the specified url, it states: localhost refused to connect.
I have downgraded the test to the most basic version.
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.Test;
public class MyTest {
@Test
public void test() {
WireMockServer server = new WireMockServer(options().bindAddress("127.0.0.1").port(6001));
server.stubFor(get(urlEqualTo("/file"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/plain")
.withBody("Hello world")));
server.start();
assertTrue(server.isRunning());
System.out.println("I put a breakpoint on this line");
}
}
I have tried different options with @WireMockTest as well (and tried about every tutorial I could find), with the same result.
When I inspect the WireMockServer object in debug mode, it shows: server.httpServer.jettyServer = Server@429064e8{STARTED}[9.4.49.v20220914]
So at least it looks like the server is started. Any ideas?
Btw: When I run wiremock standalone on the same port, it can be accessed just fine.
答案1
得分: 1
After some further investigation, it turns out the debug mode seems to be the problem. When I replace my breakpoint with an indefinite Thread.sleep(), I was able to access the Wiremock server just fine.
I got the idea from an issue reported to WireMock.Net, a C# project that mimics the Java version WireMock:
https://github.com/WireMock-Net/WireMock.Net/issues/510
As k314159 pointed out in the comments, the debugger suspends ALL threads of the test, including the one started by WireMock. To avoid this (in IntelliJ at least) you can edit the breakpoint by right-clicking it, and stating that only the current Thread should be suspended.
英文:
After some further investigation, it turns out the debug mode seems to be the problem. When I replace my breakpoint with an indefinite Thread.sleep(), I was able to access the Wiremock server just fine.
I got the idea from an issue reported to WireMock.Net, a C# project that mimics the Java version WireMock:
https://github.com/WireMock-Net/WireMock.Net/issues/510
As k314159 pointed out in the comments, the debugger suspends ALL threads of the test, including the one started by WireMock. To avoid this (in IntelliJ at least) you can edit the breakpoint by right-clicking it, and stating that only the current Thread should be suspended.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论