0、如果启动Wire Mock后没有反应,比如没有生成__file和mappings文件夹,则可能路径不对,启动的时候需要指定参数–root-dir,后面接Jar包的文件夹路径,启动后就可以生成这两个文件夹,然后在文件夹里面加入文件,重启后也要加入这个参数,才能正确找到,并启动成功:
//比如说Wire Mock的位置:C:\WireMock\wiremock-standalone-2.20.0.jar,启动例程:
java -jar C:\WireMock\wiremock-standalone-2.20.0.jar -port 9999 --root-dir C:\WireMock
1、启动时加上参数:–global-response-templating,即可以让全局模板生效。比如以下代码,生成的response,就会将{{request.path.[0]}}部分替代为访问的映射,也就是urlPath。所以,将返回:Your request url is templated:
{
"request": {
"urlPath": "/templated"
},
"response": {
"body": "Your request url is {{request.path.[0]}}",
"transformers": ["response-template"]
}
}
2、比如想在返回的内容里面加上请求体的Json的某个值,比如请求体Request Body是一个Json,我要它的属性,比如Json.name.firstName,则可以照着以下方法,就可以将想要的值从{{ }}中获取:
"The first name is {{jsonPath request.body '$.name.firstName'}}"
3、我们也可以从请求体里面获取想要的参数,比如从RequestBody里面的xml、Json里面提取数据,可以参考以下来自官方的案例:
//Json版本:
{
"outer": {
"inner": "Stuff"
}
}
//获取某一个值:
{{jsonPath request.body '$.outer.inner'}} ==> Stuff
//获取Json的一个子集:
{{jsonPath request.body '$.outer'}} ==> { "inner": "Stuff" }
//XML版本
<outer>
<inner>Stuff</inner>
</outer>
//获取某一个值:
{{xPath request.body '/outer/inner/text()'}} ==> Stuff
//获取XML的一个子XML:
{{xPath request.body '/outer/inner'}} ==> <inner>Stuff</inner>
4、以下方法,可以从Request Body里面的值来进行判断,辅助匹配:
{
"request": {
"method": "POST",
"urlPath": "/phoneNumber/validate",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.userinfo[?(@.phone == '111111')]"
} ]
},
"response": {
"status": 200,
"body": "This is a valid phone number!"
}
}
//如果请求体像是如下的Json,则可匹配:
{
"userinfo": {
"id": "abcdefg",
"phone": "111111"
}
}
{
"userinfo": {
"phone": "111111"
}
}
//以下则不可匹配:
{
"userinfo": {
"phone": "222222"
//其他成员
}
}
{
"userinfo": {
"iPhone": "111111"
//其他成员
}
}
{
"userinfo": {
"iPhone": "222222" //这个更离谱
//其他成员
}
}
5、还可以在Response Body里面插入当前时间的字符串(format属性的定义方法,跟Java里面的SimpleDateFormat类似),还可以设置时间偏移,比如昨天的这个时候,甚至还可以设置时区等,官方案例如下:
{
"request": {
"method": "GET",
"url": "/time/test"
},
"response": {
"status": 200,
"body": "Current time:{{now format='yyyy-MM-dd HH:mm:ss'}} | Yesterday time:{{now format='yyyy-MM-dd HH:mm:ss' offset='-1 days'}}"
}
}
//访问该映射,获得如下Response Body:
Current time:2019-01-25 10:31:59 | Yesterday time:2019-01-24 10:31:59
6、WireMock还可以生成随机的字符串,官方案例:
//生成33位的小写字母和数字混合字符串
{{randomValue length=33 type='ALPHANUMERIC'}}
//生成12位的大写字母和数字混合字符串
{{randomValue length=12 type='ALPHANUMERIC' uppercase=true}}
//生成55位小写字母字符串
{{randomValue length=55 type='ALPHABETIC'}}
//生成27位大写字母字符串
{{randomValue length=27 type='ALPHABETIC' uppercase=true}}
//生成10位数字字符串
{{randomValue length=10 type='NUMERIC'}}
//生成5位字母、数字和其他字符的字符串
{{randomValue length=5 type='ALPHANUMERIC_AND_SYMBOLS'}}
//生成UUID字符串
{{randomValue type='UUID'}}
7、Wire Mock还可以在Intellij IDEA上面做测试,首先引入以下依赖:
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.20.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.18.0</version>
<scope>test</scope>
</dependency>
然后就可以编写单元测试的代码了,单元测试后面需要让程序等待,否则程序会马上结束:
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.Test;
public class Test1 {
@Rule
public WireMockRule wireMockRule = new WireMockRule(19999);
@Test
public void testWireMock() {
stubFor(get(urlMatching("/your/([a-z]*)\\?and=query"))
.willReturn(aResponse()
.withHeader("Content-Type", "text/plain")
.withBody("urlMatching!")));
stubFor(post(urlEqualTo("/some/other"))
.willReturn(aResponse()
.withHeader("Content-Type", "text/plain")
.withBody("Post requested context!")));
try {
Thread.sleep(300000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
单元测试跑起来之后,会等待300秒的时间让我们去测。我们就可以通过诸如Postman发送请求,也会获得以下结果:
GET localhost:19999/your/alphabet?and=query ===> urlMatching!
POST localhost:19999/some/other ===> Post requested context!