WireMock introduction
WireMock is an Http emulation service with a Web service at its core. WireMock can provide a fixed response to a specific request (stubbing) and capture incoming requests, So that you can later verify that WireMock can be used in a project as a third-party library (typically for integration testing) or as a separate process startup (Singleton), this article begins by describing how to use the Singleton
Set up the WireMock service
To set up the WireMock service locally, first download the latest JAR from the WireMock Release page, and then start the WireMock service through the Java-jar
$Java - jar wiremock - standalone - 2.27.2. JarCopy the code
You can also specify some startup parameters, for example –port specifies the startup port of the service. For more parameters, see wiremock.org/docs/runnin…
WireMock use
When the WireMock service is started, by default, WireMock creates two folders named mappings and __Files in the current path, which are used to store mapping configuration and service files
Stubbing registered
Since you are simulating a service, you must simulate some request response. You can register a stubbing (stubbing) request body to the WireMock service by calling the http://
:
{
"request": {
"method": "GET"."url": "/some/thing"
},
"response": {
"status": 200."jsonBody": {
"name": "huangxy"."age": 12
},
"headers": {
"Content-Type": "application/json"}}}Copy the code
When the GET method calls the /some/thing interface of the WireMock service, the service returns 200. The response body is {“name”:”huangxy”,”age”:12}. The response header is Content-Type: application/json, indicating that Stubbing is successfully registered
$ curl http://localhost:8080/some/thing
{"name":"huangxy","age":12}
Copy the code
In addition to GET, WireMock supports common request methods such as POST, PUT, DELETE, HEAD, OPTIONS TRACE, and even ANY
Register stubbing by calling the interface, simply putting stubbing into memory, and stubbing will be cleaned up after the service restarts. To persist stubbing, use the mappings folder. WireMock will scan all.json files in the mappings directory at startup and register the mapping information in the files with the service
For example, we would create a hello.json file under the mappings folder, which would look like this:
{
"request": {
"method": "ANY"."url": "/hello"
},
"response": {
"status": 200."body": "Hello WireMock!"."headers": {
"Content-Type": "text/plain"}}}Copy the code
Use curl to access the/Hello interface
$ curl http://localhost:8080/hello
Hello WireMock!
Copy the code
As you can see, even if we did not invoke the __admin/ Mappings interface registration/Hello interface, the service correctly returns the expected result, proving that the service did read the contents of the mappings folder at startup
File Serving
When using GET to request WireMock, the service will first look for matching stubbing and respond to the matching request. If the matching request does not match, WireMock will look for matching resources in the __files folder. If so, return the matched resource to the caller. We create an index.html file in the __files folder with the following contents:
Hello WireMock!
Copy the code
Restart WireMock, access the /index interface, WireMock returns the contents of the index.html file – Hello WireMock! To prove that the service does read the resources in the __files directory and return the contents of the file when it does not match stubbing
Request to check
WireMock logs all requests received during the run into memory. This allows you to verify that any requests were received, and to obtain details about how many times the request was called using WireMock’s /__admin/requests/count endpoint. Let’s say we register stubbing for a /hello GET request
{
"request": {
"method": "GET"."url": "/hello"
},
"response": {
"status": 200."body": "hello wiremock"}}Copy the code
/hello is then requested twice, and GET calls the /__admin/request/count port to GET the number of times /hello is called
{
"method": "POST"."url": "/hello"
}
Copy the code
The service returns the number of times /hello was called on success
{
"count": 2."requestJournalDisabled": false
}
Copy the code
conclusion
This article only introduces the simple use of WireMock, WireMock also has more powerful functions, such as regular expression support, return specific status codes (4xx, 5XX), if you want to learn more about WireMock official website for more usage