在平时的接口测试工作中,系统的业务很少是孤立存在的,大部分业务相互之间都是需要关联调用的。 前端与后端的相互协作实现业务逻辑,接口或服务之间相互协作,或者使用第三方服务等。通常的处理方 式是搭建一个临时的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-server:mockserver-maven-plugin:5.11.1:runForked 停止: mvn -Dmockserver.serverPort=1080 org.mock-server:mockserver-maven-plugin:5.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:
- mockserver-netty - an HTTP(S) web server that mocks and records requests and responses
- mockserver-netty:jar-with-dependencies - mockserver-netty (as above) with all dependencies embedded
- mockserver-war - a deployable WAR for mocking HTTP(S) responses (for any JEE web server)
- mockserver-proxy-war - a deployable WAR that records requests and responses (for any JEE web server)
- mockserver-maven-plugin - a maven plugin to start, stop and fork MockServer using maven
- mockserver-client-java - a Java client to communicate with both the server and the proxy
In addition MockServer SNAPSHOT artifacts can also be found on Sonatype.
NPM Registry contains the following module:
- mockserver-node - a Node.js module and Grunt plugin to start and stop MockServer
- mockserver-client-node - a Node.js client for both the MockServer and the proxy
Docker Hub contains the following artifacts:
- MockServer Docker Container - a Docker container containing the Netty MockServer and proxy
- MockServer Helm Chart - a Helm Chart that installs MockServer to a Kubernetes cluster, available versions:
- mockserver-client-ruby
- Ruby client for both the MockServer and the proxy
- mockserver-client-java - a Java client for both the MockServer and the proxy
- mockserver-client-node - a Node.js and browser client for both the MockServer and the proxy
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:
- MockServer version (i.e. 5.11.2)
- How your running the MockServer (i.e maven plugin, docker, etc)
- MockServer log output, at INFO level (or higher)
- What the error is
- What you are trying to do
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.