使用WireMock快速伪造RESTful服务

⒈下载WireMock独立运行程序

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

⒉运行

java -jar wiremock-standalone-2.22.0.jar --port 7777

使用WireMock快速伪造RESTful服务

⒊项目中导入WireMock依赖

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

⒋添加模拟请求映射

 package cn.coreqi.security.wiremock;

 import com.github.tomakehurst.wiremock.client.WireMock;
import org.aspectj.util.FileUtil;
import org.springframework.core.io.ClassPathResource; import java.io.IOException; public class MockServer {
public static void main(String[] args) throws IOException {
WireMock.configureFor(7777); //告诉程序WireMock的服务端口
WireMock.removeAllMappings(); //把以前的所有配置清空 mock("/order/1","01");
}
public static void mock(String url,String fileName) throws IOException {
ClassPathResource resource = new ClassPathResource("/mock/response/"+fileName+".txt");
String content = FileUtil.readAsString(resource.getFile());
WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo(url)).willReturn(WireMock.aResponse().withBody(content).withStatus(200)));
}
}
上一篇:既使用maven编译,又使用lib下的Jar包


下一篇:MyEclipse8.5优化