WireMock快速生成接口

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.增加依赖


    com.github.tomakehurst
    wiremock
    2.19.0

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)//设置请求的响应
                )
        );
    }
}

你可能感兴趣的:(WireMock快速生成接口)