如何使用 wiremock 模拟服务

How to mock a service using wiremock

我正在尝试使用 Wiremock 模拟我的 ServiceA。但似乎缺少一些设置。我收到此错误:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /__files/serviceA/endpointA Reason:
<pre>    Not Found</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.4.v20170414</a><hr/>

</body>
</html>

404 表示未找到端点位置。

是否有任何分步教程可以解释如何设置服务?

谢谢。

有一些不同的方式:

  1. 运行 命令行中的独立进程: java -jar wiremock-standalone-2.8.0.jar(见 http://wiremock.org/docs/running-standalone/)
  2. 创建一个具有以下依赖项的简单 Maven 项目:

    <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock</artifactId>
        <version>2.8.0</version>
    </dependency>
    

    然后创建一个 class 的主要 运行 就这样:

    private static final WireMockServer server = new WireMockServer(options().port(8080));
    
    public static void main(String[] args) {
        server.stubFor(post(urlEqualTo("/localsave")).willReturn(aResponse().withStatus(HttpStatus.SC_OK)));
        server.start();
        //do some stuff
    }
    
  3. 使用 JUnit 规则:

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(options().port(8888).httpsPort(8889));
    

您可以在这里找到所有信息:http://wiremock.org/docs/

如果这些信息对您来说足够了,请告诉我。