Java WireMockRule在集成测试中不适用于非本地主机
我试图使用WireMockRule模拟远程主机的请求,但我被卡住了。目前我正在模拟本地主机的请求,如下所示:Java WireMockRule在集成测试中不适用于非本地主机,java,spring,wiremock,integration-testing,Java,Spring,Wiremock,Integration Testing,我试图使用WireMockRule模拟远程主机的请求,但我被卡住了。目前我正在模拟本地主机的请求,如下所示: @Rule public WireMockRule wireMockRule = new WireMockRule(80); ... wireMockRule.stubFor(post(urlPathEqualTo("/api/users")) .willReturn(aResponse() .withStatus(200))); 这些功能对于local
@Rule
public WireMockRule wireMockRule = new WireMockRule(80);
...
wireMockRule.stubFor(post(urlPathEqualTo("/api/users"))
.willReturn(aResponse()
.withStatus(200)));
这些功能对于localhost正常工作。然而,当我试着做类比的时候
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().bindAddress("remote").port(80));
它不起作用,事件测试无法启动。所以我想问你们,也许有人会知道我做错了什么?谢谢
WireMockRule
总是在本地主机上启动服务器。它不是为连接到远程WireMock服务器而设计的。你可以从源头上看到这一点
要将WireMock客户端配置为针对远程服务器运行,请不要使用@Rule
。只需放入WireMock.configureFor(“my.remote.host”,8000)在初始化测试时,在构造函数中编写>代码
通常,无论您是否使用@Rule
,WireMock的设计者都希望您使用WireMock
类上的静态方法,而不是WireMockRule
对象的成员方法:
import static com.github.tomakehurst.wiremock.client.WireMock.*;
...
stubFor(post(urlPathEqualTo("/api/users"))
.willReturn(aResponse()
.withStatus(200)));
但我需要模拟对不存在的主机的请求(当我运行集成测试时,主机处于脱机状态)。。有可能吗?如果你想在远程主机上启动WireMock服务器,你必须自己启动。WireMock没有为您提供在远程主机上启动服务器的功能。