使用moco的global设置功能拆分配置文件

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载自夜明的孤行灯

本文链接地址: https://www.huangyunkun.com/2014/11/17/moco-global-config/

Moco是一个很方便的小工具,可以作为开发时临时的虚假服务端。

Moco的响应方式主要通过config.json来控制。有新的需要时直接在文件中添加删除就行了。

不过长此以往这个文件就越来越大了,比如1000+行。

还好的是moco提供了global setting这个功能可以做到配置文件拆分。

配置修改

之前的配置文件看起来应该是这样的:

[
{
"request":{
"uri":"/foo"
},
"response":{
"text":"foo"
}
},
{
"request":{
"uri":"/bar"
},
"response":{
"text":"bar"
}
}
]

我希望将这两个请求拆分到两个不同的文件中。

[
{
"include":"foo.json"
},
{
"include":"bar.json"
}
]

然后在foo.json文件中添加:

[
{
"request":{
"uri":"/foo"
},
"response":{
"text":"foo"
}
}
]

bar.json类似。

这样通过moco的-g参数启动就可以了。

java -jar moco-runner-<version>-standalone.jar start -p 12306 -g settings.json

moco-maven-plugin

一般项目中还是使用插件来启动moco的,而GarrettHeel的这款插件可以满足我们的需要。

<plugin>
<groupId>com.garrettheel</groupId>
<artifactId>moco-maven-plugin</artifactId>
<version>0.9.3</version>
<configuration>
<port>8081</port>
<globalFile>settings.json</globalFile>
</configuration>
</plugin>

因为moco一般用于回归测试或者功能性测试,所以在maven的生命周期中一般配置为

<plugin>
<groupId>com.garrettheel</groupId>
<!-- ... -->
<executions>
<execution>
<id>start-moco</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-moco</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

参考资料

moco-maven-plugin

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载自夜明的孤行灯

本文链接地址: https://www.huangyunkun.com/2014/11/17/moco-global-config/

发表评论