San*_*Rey 10 java junit mockito spring-boot wiremock
我在 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"))));
Run Code Online (Sandbox Code Playgroud)
但是当我运行测试时,我在这一行出现了这个错误:
stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn(..
Run Code Online (Sandbox Code Playgroud)
和错误:
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
Run Code Online (Sandbox Code Playgroud)
mic*_*alk 10
从WireMock
文档:
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
...
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8888));
Run Code Online (Sandbox Code Playgroud)
@Before
):import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
...
WireMockServer wm = new WireMockServer(options().port(8888));
wm.stubFor(...);
wm.start();
Run Code Online (Sandbox Code Playgroud)
WireMock.configureFor(8888);
Run Code Online (Sandbox Code Playgroud)
将我之前的评论发布为答案,因为它似乎对一些人有所帮助。谢谢@jmrah。:)
对于Kotlin和JUnit5,可以通过将实际WireMockServer
实例添加到stubFor
或verify
方法调用来解决此问题。
wireMockServer.stubFor()
Run Code Online (Sandbox Code Playgroud)
或者
wireMockServer.verify()
Run Code Online (Sandbox Code Playgroud)
添加此后,测试应该可以工作。
归档时间: |
|
查看次数: |
11561 次 |
最近记录: |