使用WireMock快速伪造RESTful服务
⒈下载WireMock独立运行程序
http://wiremock.org/docs/running-standalone/
⒉运行
java -jar wiremock-standalone-2.22.0.jar --port 7777
⒊项目中导入WireMock依赖
1 <dependency> 2 <groupId>com.github.tomakehurst</groupId> 3 <artifactId>wiremock</artifactId> 4 <version>2.22.0</version> 5 </dependency>
⒋添加模拟请求映射
1 package cn.coreqi.security.wiremock; 2 3 import com.github.tomakehurst.wiremock.client.WireMock; 4 import org.aspectj.util.FileUtil; 5 import org.springframework.core.io.ClassPathResource; 6 7 import java.io.IOException; 8 9 public class MockServer { 10 public static void main(String[] args) throws IOException { 11 WireMock.configureFor(7777); //告诉程序WireMock的服务端口 12 WireMock.removeAllMappings(); //把以前的所有配置清空 13 14 mock("/order/1","01"); 15 } 16 public static void mock(String url,String fileName) throws IOException { 17 ClassPathResource resource = new ClassPathResource("/mock/response/"+fileName+".txt"); 18 String content = FileUtil.readAsString(resource.getFile()); 19 WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo(url)).willReturn(WireMock.aResponse().withBody(content).withStatus(200))); 20 } 21 }