Wiremock在调试时拒绝连接。

huangapple go评论74阅读模式
英文:

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.Wiremock在调试时拒绝连接。

huangapple
  • 本文由 发表于 2023年6月8日 15:28:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定