我正在尝试测试对HTTP服务器具有基础GET调用的代码。
我正在尝试使用WireMock,并基于“入门”,我有以下代码:
@Rule
public WireMockRule wireMockRule =
new WireMockRule(WireMockConfiguration.wireMockConfig().port(8888)); // No-args constructor defaults to port 8080
@BeforeClass
public static void beforeClass(){
stubFor(get(urlEqualTo("/Path/Get"))
.willReturn(aResponse()
.withStatus(200)
.withBody("[1]")));
}
@Test
public void testGetRanking() throws Exception {
Fetcher fetcher = new Fetcher("http://localhost:8888",null);
int rankinganking = fetcher.getRanking("Max");
Assert.assertEquals(1, ranking);
}
Run Code Online (Sandbox Code Playgroud)
运行测试时,我得到以下堆栈跟踪:
wiremock.org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused
at wiremock.org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at wiremock.org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at wiremock.org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at wiremock.org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
...
Run Code Online (Sandbox Code Playgroud)
该错误涉及端口8080,但我将端口配置为8888。我知道默认情况下WireMock以端口8080开头,因此这可能是其内部配置存在问题。
这是什么问题?
我正在使用wiremock来模拟 Web服务.我正在使用它来提供对我们正在测试的某些单元的服务访问.所以基本上我们有多个junit测试,它们使用来自wiremock的存根.
但是当我们一次运行多个测试时(假设运行一个完整的test.java文件),它会在完成一些测试用例后继续等待.以下内容将显示在日志的底部,
[qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0]
DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop waiting on select
Run Code Online (Sandbox Code Playgroud)
Junit版本:4.12
Wiremock版本:2.5.0(wiremock-standalone)
Java版本:1.8.0_77
我还检查了这个问题.但仍然无法弄清楚如何克服这一点.
我正在使用Wiremock来删除移动客户端发出的一些API调用.
努力从请求中获取模板文件名.
任何人都可以确认这是否应该与Wiremock独立使用?
"response": { "status": 200, "bodyFileName": "Account-{{jsonPath request.body '$.user.identity'}}.json”}
Run Code Online (Sandbox Code Playgroud)
我有静态文件要返回正常,似乎bodyFileName不喜欢模板.
例:
我用以下Json配置wm:
{
"request": {
"method" : "GET",
"url": "/users/D8428899330"
},
"response": {
"status": 200,
"bodyFileName": "user-{{request.path.[1]}}.json"
}
}
Run Code Online (Sandbox Code Playgroud)
然后当我尝试url时,http:// localhost:9696/users/D8428899330我收到以下错误,
HTTP错误:500访问/ users/D8428899330时出现问题.原因:
了java.lang.RuntimeException:java.io.FileNotFoundException:/Users/iainframe/Documents/__files/user-{{request.path.[1]}}.json(没有这样的文件或目录)
应该表明文件user-D8428899330.json resides位于正确的位置,因为我已对其进行了硬编码并将其恢复正常.启动WireMock的命令是:
java -jar ~/Documents/wm.jar --port 9696 --global-response-templating --verbose --root-dir /Users/iainframe/Documents/
Run Code Online (Sandbox Code Playgroud) 我有一些使用WireMock的Junit测试。测试在本地通过,但是在詹金斯上,我得到了一个错误
java.lang.RuntimeException: java.net.BindException: Address already in use
在WireMock配置中,我进行了设置dynamicHttpsPort()(根据文档“ 在启动时随机分配HTTPS端口 ”)及其在本地工作,但是在Jenkins上失败。
我的WireMock配置是
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig()
.dynamicHttpsPort()
.keystorePath(certsDir.resolve("server.jks").toString())
.keystorePassword(MY_PASS)
.keystoreType("JKS")
);
Run Code Online (Sandbox Code Playgroud)
我有2个JUnit类,在其中创建了一个new WireMockRule,它们可以同时运行。在其中一门课程中,我有4个测试,在另一门课程中,我有一个测试。所有5个测试均失败。
完整的堆栈跟踪为:
com.github.tomakehurst.wiremock.common.FatalStartupException: java.lang.RuntimeException: java.net.BindException: Address already in use
at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:146)
at com.github.tomakehurst.wiremock.junit.WireMockRule$1.evaluate(WireMockRule.java:68)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at …Run Code Online (Sandbox Code Playgroud) 我有一个 Spring Boot 微服务的集成测试。问题是该服务在启动时调用外部服务(通过 REST)。我正在使用 WireMock 来模拟调用。Spring 使应用程序在 WireMock 启动之前启动。因此,rest 调用失败,服务也失败。
这个电话是由我们公司的一个图书馆发出的,所以我不能在那里改变任何东西。
你对我有什么建议吗?
我正在尝试使用 WireMock 创建动态模拟。我有一种情况,如果我指定 URL 如下:
http://localhost:8089/api/account/abc@abc.com
Run Code Online (Sandbox Code Playgroud)
那么我应该收到如下回复:
{
"account" : "abc@abc.com"
}
Run Code Online (Sandbox Code Playgroud)
简而言之,路径参数在响应正文中返回。我可以使用urlPathPatternset to 使请求 URL 通用,/api/account/([a-z]*)但是我不确定我应该如何abc@abc.com使用正则表达式在响应中捕获和返回它。
我已经通过示例 REST/HTTP 请求模拟实现了一个基本的 WireMock。服务器代码实现如下。
使用此代码,当我从 Postman 发出 GET 请求(即 GET http://127.0.0.1:8089/some/thing)时出现以下错误。
由于此 WireMock 实例中没有存根映射,因此无法提供响应。
我的设置/代码中缺少什么?
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
public class MockApp {
private WireMockServer wireMockServer;
public MockApp(String testSpec) {
wireMockServer = new WireMockServer(WireMockConfiguration.options().
port(8089).
usingFilesUnderDirectory(testSpec).
disableRequestJournal());
}
public void start() {
wireMockServer.start();
}
public void stop() {
wireMockServer.stop();
}
}
Run Code Online (Sandbox Code Playgroud)
主要功能是:
public class MockMain {
public static void main(String[] args) {
String baseDir = System.getProperty("user.dir");
String testResource = baseDir + "/resources/testconfig/";
MockAMS mockAMS = new MockAMS(testResource);
mockAMS.start();
} …Run Code Online (Sandbox Code Playgroud) 我们的一个外部 API 使用带有特殊字符的查询参数名称(不要问我为什么,我不知道)。我的这个 API 的假客户端方法声明如下:
@GetMapping("/v1/user/{userId}/orders")
List<Order> getOrders(
@PathVariable("userId") String userId,
@RequestParam("page[number]") Integer pageNumber,
@RequestParam("page[size]") Integer pageSize);
Run Code Online (Sandbox Code Playgroud)
正如我提到的,请求参数包含特殊字符[和]. 我正在使用 Spock 进行测试,我想像这样设置 Wiremock 存根:
wiremock.stubFor(
get(urlPathMatching('/v1/users/' + userId + '/orders'))
.withQueryParam("page[number]", new EqualToPattern("1"))
.withQueryParam("page[size]", new AnythingPattern())
.willReturn(
status(200)
.withHeader("Content-type", "application/json")
.withBody("""[]""")
))
Run Code Online (Sandbox Code Playgroud)
但我得到:
--------------------------------------------------------------------------------------------------
| Closest stub | Request |
--------------------------------------------------------------------------------------------------
|
GET | GET
/v1/users/123/orders | /v1/users/123/orders?page%5Bnumber%5D=%7Bpage%5Bnumber%5
| D%7D&page%5Bsize%5D=%7Bpage%5Bsize%5D%7D
|
Query: page[number] = 1 | <<<<< Query is not present
Query: page[size] [anything] (always) | <<<<< …Run Code Online (Sandbox Code Playgroud) 是否有人在记录服务器响应方面有完整的,最新的教程或项目-根据需要获取,发布和标头,以及使用Wiremock和/或模拟Web服务器播放它们?
我已经看了很多
我有一个Wiremock服务器的独立实例。映射作为json文件存储在mappings文件夹下。我有一个POST请求,需要在响应中返回动态ID(整数)。有没有办法在json文件中配置它?
wiremock ×10
java ×4
junit ×2
spring-boot ×2
android ×1
feign ×1
jenkins ×1
spring ×1
stubbing ×1
unit-testing ×1