WireMock的使用
前期對(duì)接口隔心,但接口還未開發(fā)時(shí)使用篷扩。接口數(shù)據(jù)為虛擬的变汪。
使用步驟
1.下載jar
選擇Docs->Running as a Standalone Process-> downloaded the standalone JAR
2.運(yùn)行
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.代碼連接服務(wù)
public class MockServer {
public static void main(String[] args) throws IOException {
WireMock.configureFor(8062);//設(shè)置端口
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)//設(shè)置請(qǐng)求的地址
).willReturn(
WireMock.aResponse().withBody(content).withStatus(200)//設(shè)置請(qǐng)求的響應(yīng)
)
);
}
}