当前位置:   article > 正文

wireMock快速伪造restful服务

wiremock 和微服务

官网地址:http://wiremock.org/

Jar下载:http://repo1.maven.org/maven2/com/github/tomakehurst/wiremock/1.57/wiremock-1.57-standalone.jar

下载wiremock jar工具

jar下载完毕后,启动jar,

java -jar wiremock-1.57-standalone.jar –port 9999 --verbose

 

配合springboot项目,能够随时更新mockservice

相关依赖:

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

新建mock服务:

package com.nxz.wiremock;

import com.github.tomakehurst.wiremock.client.WireMock;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

/**
 * 启动mockserver服务
 */
public class MockServer {

    public static void main(String[] args) throws IOException {
        WireMock.configureFor(9999);//指定端口
        WireMock.removeAllMappings();//清空之前的配置

        //每调用一次mock方法,就代表提供了一个mockservice接口
     mock(
"/order/1", "01.txt"
); mock("/order/2", "02.txt"
); } private static void mock(String url, String file) throws IOException {
    //mock\\response\\+file 这里指定的是返回的json数据 ClassPathResource resource
= new ClassPathResource("mock\\response\\" + file); String content = StringUtils.join(FileUtils.readLines(resource.getFile(), "UTF-8").toArray()); WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo(url)) .willReturn(WireMock.aResponse().withBody(content).withStatus(200))); } }

模拟返回数据:

{
    "aaa":1,
    "type":"a"
}

 

转载于:https://www.cnblogs.com/nxzblogs/p/10793310.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/217029
推荐阅读
相关标签
  

闽ICP备14008679号