Mock Server的作用:对于特定的Request请求,返回特定的Response响应。
在平时的接口测试工作中,系统的业务很少是孤立存在的,大部分业务相互之间都是需要关联调用的。
前端与后端的相互协作实现业务逻辑,接口或服务之间相互协作,或者使用第三方服务等。通常的处理方
式是搭建一个临时的server,模拟服务,提供数据进行联调测试。
Mock Server的作用:对于特定的Request请求,返回特定的Response响应。Mock数据的方式常用
的有两种。
方式一:Java API以编程方式,如下:
new MockServerClient("localhost", 1080)
.when(
request()
.withMethod("POST")
.withPath("/login")
.withBody("{username: 'foo', password: 'bar'}")
)
.respond(
response()
.withStatusCode(200)
.withCookie(
"sessionId", "2By8LOhBmaW5nZXJwcmludCIlMDAzMW"
)
.withHeader(
"Location", "https://www.mock-server.com"
)
);
方式二:json方式,如下:
{
"httpRequest": {
"path": "/api/user/outer/query/userStatus",
"method": "GET"
},
"httpResponse": {
"body": {
"state" : "OK",
"body" : 1,
"requestId" : "b8fe75be1a0647b09294143e0697ee9d"
}
}
}
mockserver部署包文档结构:
mockserver
|——mockserver:启动脚本配置
|——conf:配置
| |——logging.properties:日志配置
| |——mock-server.json:mock数据配置
|——lib:放有jar包
|——log:日志
|——mock-server.sh:启动脚本
jar包生成步骤:
mockserver-netty包下运行如下命令:
mvn clean package -DskipTests
生成:
mockserver-netty/target/mockserver-netty-5.11.1-jar-with-dependencies.jar
Java启动:
java -jar mockserver-netty-5.11.1-jar-with-dependencies.jar -serverPort 1080,1081 -logLevel INFO -Dmockserver.initializationJsonPath=“期望配置文件”
maven插件启动:
mvn -Dmockserver.serverPort=1080 -Dmockserver.logLevel=INFO org.mock-server5.11.1:runForked
停止:
mvn -Dmockserver.serverPort=1080 org.mock-server5.11.1:stopForked
Mock Server UI:
http://localhost:1080/mockserver/dashboard
记录request/response:
curl -v -X PUT “http://localhost:1080/mockserver/retrieve?type=REQUEST_RESPONSES“
For usage guide please see: www.mock-server.com
Please see: Change Log
Chat | ![]() |
Feature Requests | ![]() |
Issues / Bugs | ![]() |
Backlog | ![]() |
Maven Central contains the following MockServer artifacts:
In addition MockServer SNAPSHOT artifacts can also be found on Sonatype.
NPM Registry contains the following module:
Docker Hub contains the following artifacts:
If you have any problems, please check the project issues and avoid opening issues that have already been fixed. When you open an issue please provide the following information:
Pull requests are, of course, very welcome! Please read our contributing to the project guide first. Then head over to the open issues to see what we need help with. Make sure you let us know if you intend to work on something. Also check out to see what is already in the backlog.
Feature requests are submitted to github issues. Once accepted they will be added to the backlog. Please check out to see what is already in the backlog.