java Wiremock 为存根 url 返回 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35819426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wiremock returning 404 for a stubbed url
提问by tuk
I have defined wireMock server as follows:-
我已经定义了wireMock服务器如下:-
private WireMockServer wireMockServer;
@Before
public void preSetup() throws Exception {
wireMockServer = new WireMockServer(56789);
wireMockServer.start();
};
@Override
@After
public void tearDown() {
wireMockServer.stop();
}
@Test
public void testSendMatchingMessage() throws Exception {
wireMockServer.stubFor(get(urlEqualTo("/orders/v1/ordersearch/"))
.willReturn(aResponse().withStatus(200).withBody("<response>Some content</response>")));
}
But whenever I am hitting the url something like below
但是每当我点击以下网址时
http://0.0.0.0:56789/orders/v1/ordersearch/?field=address%2Cfinance%2Cshipping&limit=10&page=2&q=createdFrom.gt%7E2016-01-11T10%3A12%3A13
I am getting the below error:-
我收到以下错误:-
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 404 NOT_FOUND</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /__files/orders/v1/ordersearch/. Reason:
<pre> NOT_FOUND</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
</body>
</html>
Can some one let me know what I am doing wrong?
有人可以让我知道我做错了什么吗?
回答by ivan_pozdeev
As per Stubbing - Wiremock(the 1st in Google on "wiremockserver urlequalto"):
根据Stubbing - Wiremock(Google 中“wiremockserver urlequalto”的第一个):
Note: you must use
urlPathEqualTo
orurlPathMatching
to specify the path, asurlEqualTo
orurlMatching
will attempt to match the whole request URL, including the query parameters.
注意:您必须使用
urlPathEqualTo
或urlPathMatching
来指定路径,因为urlEqualTo
orurlMatching
将尝试匹配整个请求 URL,包括查询参数。
回答by AdamMc331
For anyone who is trying to add Wiremock to an Android app and stumbles upon this question:
对于任何试图将 Wiremock 添加到 Android 应用程序并偶然发现这个问题的人:
If you run your mocking afterthe network call is made, it won't work. This may seem obvious but I was tripped up on it.
如果在进行网络调用后运行模拟,它将不起作用。这似乎很明显,但我被它绊倒了。
When you run an Espresso test, by default the activity test rule launches the activity right away, and so the activity was firing and pulling it's config data before my mocking code was actually run. I was seeing the same error as OP.
当您运行 Espresso 测试时,默认情况下,活动测试规则会立即启动活动,因此活动会在我的模拟代码实际运行之前触发并提取其配置数据。我看到了与 OP 相同的错误。
The fix is to make your activity test rule not launch initially, then mock your data, and tell the activity to start once you've done all that.
解决方法是让您的活动测试规则最初不启动,然后模拟您的数据,并在您完成所有操作后告诉活动开始。
回答by Sylhare
The one I usually use is urlPathMatching
that you can get when importing:
我通常使用的一种urlPathMatching
是您在导入时可以获得的:
import static com.github.tomakehurst.wiremock.client.WireMock.*;
So in your test, you would have your service
of class MyService
that would make a rest call to an external api "/orders/v1/ordersearch/"
which is that call that will be mocked with the wiremock stubFor
.
因此,在您的测试中,您将拥有一个对外部 api 进行休息调用service
的类MyService
,"/orders/v1/ordersearch/"
该调用将使用 wiremock 进行模拟stubFor
。
Then the mockServer.verify
that the mockServer has made a get to the external api when the service service.sendToExternalUrl()
.
然后mockServer.verify
,当服务service.sendToExternalUrl()
.
private WireMockServer mockServer;
private MyService service;
@Before
public void preSetup() {
mockServer = new WireMockServer(56789);
mockServer.start();
}
@After
public void tearDown() {
mockServer.stop();
}
@Test
public void testSendMatchingMessage() {
UrlPattern externalUrl = urlPathMatching("/orders/v1/ordersearch/");
stubFor(get(externalUrl).willReturn(aResponse().withStatus(200)));
service.sendToExternalUrl();
mockServer.verify(1, getRequestedFor(externalUrl)
.withRequestBody(containing(new JSONObject().put("orders", "results").toString())));
}