TestNG + wirelock to mock the external system interface

Posted by sqlmc on Mon, 03 Jan 2022 16:11:57 +0100

1. Introduction to wiremock

WireMock Is based on HTTP of API Simulator. 

2. Use in java interface testing framework

Wirelock supports multiple filtering conditions such as request mode / path / input parameter / header /, filtering interface and returning response. The response can be a file or any returned content defined by itself. Official documents provide a wide variety of use options: http://wiremock.org/docs/

The following is a simple example. Our system is an advertising system, which connects with the advertising supplier, which is hereinafter referred to as "Bear company". After opening an advertising account and launching, the customer needs to check the launch data. However, because the advertisement needs real delivery, there will be delivery data, otherwise the data returned by the interface will always be 0. Therefore, it is impossible to test the view delivery data interface by accessing the interface of Bear company. By changing the original request to the "Bear" interface to the wiremock service, we can return the test data we want.

  • Use [peanut shell] / [net cloud penetration] and other tools to open a domain name to point to the local port. I use 7777, which corresponds to the port number started by wirelock
  • Add dependency to test project
<dependency>
   <groupId>com.github.tomakehurst</groupId>
   <artifactId>wiremock-jre8</artifactId>
   <version>RELEASE</version>
   <scope>compile</scope>
</dependency>
  • Create a new folder in / src/test/resources:__ files, in__ files creates a new JSON file accountsearch json
{
  "code": "000000",
  "message": "success",
  "data": {
    "pageSize": 10,
    "pageNum": 1,
    "total": 1,
    "list": [
      {
        "key1": xxx,
        "key2": xxx,
        "key3": xxx,
    	xxxx
      }
    ]
  }
}
  • Add the following code to the test code:
 public WireMockServer wireMockServer;
 
 @BeforeClass(alwaysRun = true)
    public void startMock(){
        wireMockServer = new WireMockServer(wireMockConfig().port(7777));
        WireMock.configureFor("localhost", 7777);//The configuration port number is 7777
        wireMockServer.start();//Start wirelock
        stubFor(WireMock.post(urlPathEqualTo("/test/bear/searchIndicator"))//If the path of the interface requesting the port number of the local 7777 is equal to "/ test/bear/searchIndicator", the searchindicator is returned JSON this file
                .willReturn(aResponse()
                        .withHeader("Content-Type","application/json")
                        .withStatus(200)
                        .withBodyFile("searchIndicator.json")))
        ;
    }
    
...Test code...

 @AfterClass(alwaysRun = true,groups = "ads-b")
    public void endMock(){
        wireMockServer.stop();
    }
    
  • Change the "Bear company" domain name configured by the project to the wiremock service domain name

  • Start test: the test results are as follows:

3. Independent use (recommended)

Wirelock also provides wirelock - standalone Jar package can provide services separately. You only need to download it from the official website and use the startup command, Java - jar XXXXXXXX Jar -- port 7777, which can be used after startup.

The following is an example

  • Create a wirelock folder
  • In the wiremock folder, download the jar package: http://wiremock.org/docs/running-standalone/
  • Start: Java - jar wirelock-standalone-2.26.3 jar -port 7777
  • Observe the wiremock folder and generate two folders: mappings and__ files
  • Enter the mappings folder and create a new file searchindicator JSON, content:
{
  "request": {
      "method": "POST",
      "urlPath": "/test/bear/searchIndicator"
  },
  "response": {
      "status": 200,
      "bodyFileName": "searchIndicatorResponse.json",
      "headers": {
          "Content-Type": "application/json"
      }
  }
}

The above "bodyFileName": "searchIndicator.json", searchindicator JSON is below__ The file names of the files created in the files folder need to be the same.

  • Enter__ files folder, create a new file searchindicatorresponse JSON, content
    :
{
    "code": "000000",
    "message": "success",
    "data": {
        "pageSize": 10,
        "pageNum": 1,
        "total": 1,
        "list": [
            {
                "key1": xxx,
                "key2": xxx,
                "key3": xxx,
         ...
            }
        ]
    }
}
  • Restart the wirelock service: after ctrl c stops, start it again
  • Trigger query https://test.bear.cn/test/bear/searchIndicator Interface and find the returned searchindicatorresponse JSON content

4. Deploy the mock service to the test server

  • Will__ Upload files and mappings to gitlab, and directly modify mock data on gitlab
  • After modification, trigger the restart of wiremock service through Jenkins:
  • Each json file in the mappings folder corresponds to an interface__ The json files in the files file are simulated return contents. Assuming that more interfaces need to be connected in the future and mock s are still required, then in mappings and__ Add a new file in files, modify the corresponding path and return content, and restart the service to take effect.

last

Wirelock is a very easy-to-use mock tool. In addition to being used in interface testing, it can also be used in front and back-end joint debugging. You only need to provide a return demo at the back-end, and the front-end development can carry out joint debugging without waiting for the code development to be completed.

If there are more and better ways to use, please leave a message in the comment area.

Topics: Java interface