java - Wiremock 错误 - 此 WireMock 实例中没有存根映射
问题描述
我已经使用示例 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();
}
}
主要功能是:
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();
}
}
在“resources/testconfig”下,有一个名为 mapping.json 的文件,其中包含:
{
"request": {
"method": "GET",
"url": "/some/thing"
},
"response": {
"status": 200,
"body": "Hello world!",
"headers": {
"Content-Type": "text/plain"
}
}
}
解决方案
我找到了解决方案。因此,基本上我们需要在“testResource”变量标识的目录下创建一个名为“mappings”(确切名称)的文件夹。所以在上面的代码示例中,mapping.json 文件将存储在位置:“MockApp/resources/testconfig/mappings/mapping.json”。
一旦完成,它将打印以下输出。从日志中可以看出,“存根映射大小为 1”。一旦您在代码中添加以下行,就会打印出来。
System.out.println("Stub mapping size: " + wireMockServer.getStubMappings().size());
Stub mapping size: 1
{
"id" : "da5735a6-b6cc-45aa-8256-fb88b5670610",
"request" : {
"url" : "/some/thing",
"method" : "GET"
},
"response" : {
"status" : 200,
"body" : "Hello world!",
"headers" : {
"Content-Type" : "text/plain"
}
},
"uuid" : "da5735a6-b6cc-45aa-8256-fb88b5670610"
}
推荐阅读
- java - Spring WebFlux - 为什么我必须等待 WebClient 响应?
- javascript - Javascript在for循环中返回对象嗨伙计
- amazon-web-services - 在 AWS Lambda 上使用 Python 从 S3 中的 AWS SES 邮件中提取电子邮件附件
- java - 填充对象数组时出现问题。结果始终为空
- php - “将 [name] 添加到可填充属性以允许对 [Illuminate\Foundation\Auth\User] 进行批量分配。”
- ejb - 使用 EJB 应用程序实现微配置文件健康检查
- loops - 批处理 – Findstr 错误级别条件,引号?
- discord - 如果消息包含某些内容,则添加反应
- raspberry-pi - 树莓派上网络硬盘的 Rsnapshot 文件权限问题
- windows - 我是否需要对从 SystemParametersInfo() 检索到的字体调用 DeleteObject()?