官网地址: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" }