我在 Junit 中有这段代码,我明确地将端口设置为 8888
when(clientUtils.getLinkUrl(eq(HOSTELS_MICROSERVICE.name()), eq(HOSTELS_MICROSERVICE.name()), anyMap()))
.thenReturn("http://localhost:8888/HOSTELS/HOSTELSMethods");
stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn(
aResponse().withStatus(200)
.withHeader("Content-Type", APPLICATION_JSON_VALUE)
.withBody(ResourceUtils.getResourceFileAsString ("__files/HOSTELS.json"))));
但是当我运行测试时,我在这一行收到此错误:
stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn(..
和错误:
wiremock.org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
对于Java用户
基于WireMock http://wiremock.org/docs/configuration/ docs.
在测试中使用 WireMock 有 3 种可能性:
- 如果您使用 Wiremock 作为 JUnit 4 规则来配置端口使用:
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
...
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8888));
- 如果您使用新实例并从测试类启动它(例如
@Before
) :
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
...
public class Test {
WireMockServer wm;
@BeforeEach
void setUp() {
wm = new WireMockServer(options().port(8888));
wm.start();
}
@Test
void test() {
wm.stubFor(...);
}
}
- 使用默认实例的静态配置(在测试中不使用新实例):
WireMock.configureFor(8888);
对于 Kotlin 用户
如果您使用 kotlin,您可以将实际的wiremock 实例添加到stubFor
and verify
打电话像wm.stubFor()
并像本答案的选项 3 中那样配置端口。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)