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

<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)//设置请求的响应
                )
        );
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容