WireMock快速生成接口

2018-12-18  本文已影响0人  61etj

WireMock的使用

前期对接口,但接口还未开发时使用。接口数据为虚拟的。

使用步骤

1.下载jar

http://wiremock.org

选择Docs->Running as a Standalone Process-> downloaded the standalone JAR

2.运行

http://wiremock.org/docs/running-standalone/

java -jar wiremock-standalone-2.19.0.jar --port 8062

3.增加依赖

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

4.代码连接服务

public class MockServer {

    public static void main(String[] args) throws IOException {
        WireMock.configureFor(8062);//设置端口
        WireMock.removeAllMappings();//每次重连都清空以前的配置

        mock("/order/1","01");
    }

    private static void mock(String url, String file) throws IOException {
        ClassPathResource resource = new ClassPathResource("mock/response/"+ file +".txt");
        String content = StringUtils.join(FileUtils.readLines(resource.getFile(),"UTF-8"),"\n");

        WireMock.stubFor(
                WireMock.get(
                        WireMock.urlPathEqualTo(url)//设置请求的地址
                ).willReturn(
                        WireMock.aResponse().withBody(content).withStatus(200)//设置请求的响应
                )
        );
    }
}
上一篇 下一篇

猜你喜欢

热点阅读