Wiremock 测试总是在一个简单的请求上得到 404
Wiremock tests always getting a 404 on a simple request
我试图在我的单元测试中通过一个简单的请求让 wiremock 达到 return 200 状态,但是,这个单元测试总是 return 出现 404 错误。
如何解决?
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertTrue;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.Test;
import java.net.HttpURLConnection;
import java.net.URL;
public class WiremockTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule(8089); // No-args constructor defaults to port 8080
@Test
public void exampleTest() throws Exception {
stubFor(get(urlPathMatching("/my/resource[0-9]+"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withBody("<response>Some content</response>")));
int result = sendGet("http://localhost/my/resource/121");
assertTrue(200 == result);
//verify(getRequestedFor(urlMatching("/my/resource/[a-z0-9]+")));
}
private int sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
return responseCode;
}
}
}
使用您提供的代码,我首先必须处理抛出的 java.net.ConnectionException。您的测试 url 需要本地主机上的端口。
sendGet("http://localhost:8089/my/resource/121")
之后,我认为您收到 404 的原因是您的正则表达式与您的测试不匹配 url。
urlPathMatching("/my/resource[0-9]+")
should be
urlPathMatching("/my/resource/[0-9]+")
note the additional path separator between 'resource' and '[0-9]+'
像 regex101 这样的正则表达式测试在线工具可以用来测试模式匹配行为。 (记得转义你的正斜杠)
模式:\/my\/resource\/[0-9]+
测试字符串:http://localhost:8089/my/resource/121
希望对您有所帮助!
我试图在我的单元测试中通过一个简单的请求让 wiremock 达到 return 200 状态,但是,这个单元测试总是 return 出现 404 错误。
如何解决?
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertTrue;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.Test;
import java.net.HttpURLConnection;
import java.net.URL;
public class WiremockTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule(8089); // No-args constructor defaults to port 8080
@Test
public void exampleTest() throws Exception {
stubFor(get(urlPathMatching("/my/resource[0-9]+"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withBody("<response>Some content</response>")));
int result = sendGet("http://localhost/my/resource/121");
assertTrue(200 == result);
//verify(getRequestedFor(urlMatching("/my/resource/[a-z0-9]+")));
}
private int sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
return responseCode;
}
}
}
使用您提供的代码,我首先必须处理抛出的 java.net.ConnectionException。您的测试 url 需要本地主机上的端口。
sendGet("http://localhost:8089/my/resource/121")
之后,我认为您收到 404 的原因是您的正则表达式与您的测试不匹配 url。
urlPathMatching("/my/resource[0-9]+")
should be
urlPathMatching("/my/resource/[0-9]+")
note the additional path separator between 'resource' and '[0-9]+'
像 regex101 这样的正则表达式测试在线工具可以用来测试模式匹配行为。 (记得转义你的正斜杠)
模式:\/my\/resource\/[0-9]+
测试字符串:http://localhost:8089/my/resource/121
希望对您有所帮助!