gpt4 book ai didi

java - Wiremock 错误 - 此 WireMock 实例中没有 stub 映射

转载 作者:搜寻专家 更新时间:2023-11-01 03:15:35 26 4
gpt4 key购买 nike

我已经实现了一个带有示例 REST/HTTP 请求模拟的基本 WireMock。服务器代码实现如下。

使用此代码,当我从 Postman 发出 GET 请求(即 GET http://127.0.0.1:8089/some/thing )时出现以下错误。

无法响应,因为此 WireMock 实例中没有 stub 映射。

我的设置/代码中缺少什么?

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”。

完成后,它将打印以下输出。从日志中可以看出,“ stub 映射大小为 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 - Wiremock 错误 - 此 WireMock 实例中没有 stub 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54722250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com