WireMock - Powerful Analog Test Tool

brief introduction

It is officially stated that WireMock is an HTTP-based API emulator that simulates your API for fast, powerful, and comprehensive testing. Its core is the Web server, which can launch to provide a canned response (stub) to a specific request and capture incoming requests for later inspection (validation), and all WireMock's functions can pass through its REST(JSON) interfaces and Java API s for access.In addition, you can configure stubs through JSON files.

Maven

<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock-jre8</artifactId>
    <version>2.25.1</version>
    <scope>test</scope>
</dependency>

Standalone Server

WireMock servers can run in their own processes and be configured through Java API s, JSON on HTTP, or JSON files.Once you have Download stand-alone JAR And run it by command:

java -jar wiremock-standalone-2.25.1.jar

Command Line Options

You can add the specified content to the command line, eg:

--port: Set the HTTP port number, eg:--port 8888

--https-port: If specified, HTTPS is enabled on the port provided.Note: When you specify this parameter, WireMock will still be separately bound to the HTTP port (default is 8080).Therefore, when running multiple WireMock servers, you also need to specify the--port parameter to avoid conflicts.

Startup is like this:

After starting wiremock, let's write a MockClient client to connect to the wiremock server:


/**
 * Connect to the WireMock server we started
 *
 * @Author Lin Bizhao
 * @Date 2019/11/5 15:59
 */
public class MockClient {

    public static void main(String[] args) throws IOException {
        WireMock.configureFor(8888);    //Specify Port
        WireMock.removeAllMappings();        //Empty last release
        //Send a GET request, specify url as/user/1, and return the json content specified for withBody
        WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/user/1"))
                .willReturn(WireMock.aResponse().withBody("{\"id\":1,\"username\":\"jacklin\"}")));

    }
}

Run the main function and access http://localhost:8888/user/1 And http://localhost:8888/manage/user/login.do And observe the results as follows:

Now that wiremock works the way we want it to, we can customize a number of returned json format strings and use wiremock simulation to return the data we want. Now we encapsulate it as a mock method, which can be used by all our API s to simulate request returns:

/**
 * Connect to the WireMock server we started
 *
 * @Author Lin Bizhao
 * @Date 2019/11/5 15:59
 */
public class MockClient {

    public static void main(String[] args) throws IOException {
        WireMock.configureFor(8888);    //Specify Port
        WireMock.removeAllMappings();        //Empty last release

        mock("/user/1", "test1");
        mock("/manage/user/login.do","login");
    }

    private static void mock(String url, String file) throws IOException {
        ClassPathResource resource = new ClassPathResource("mock/response/" + file + ".txt");
        //Connect content to string
        String content = StringUtils.join(FileUtils.readLines(resource.getFile(), "UTF-8").toArray(), "\n");

        //Send a GET request, specify url as/user/1, and return the json content specified for withBody
        WireMock.stubFor(WireMock.get(WireMock.urlEqualTo(url))
                .willReturn(WireMock.aResponse().withBody(content).withStatus(200)));

        WireMock.stubFor(WireMock.post(WireMock.urlEqualTo(url)).willReturn(WireMock.aResponse().withBody(content).withStatus(200)));
    }
}

We create our txt file in the project classpath to hold our data, which allows us to quickly simulate the various requests we want and the specified return contents, so that you can use this tool to simulate a whole set of services in a very short time, so that the front end can be developed from your set of services:

WireMock Request Matching

Web address matching

URLs can be matched using either fully consistent or regular expressions, or you can choose to match only the part of the URL's path or the matched path and query it.

Equal Matching on Path and Query

Java:

urlEqualsTo("/your/url?and=query")

Response JSON

{
  "request": {
    "url": "/your/url?and=query"
    ...
  },
  ...
}

Regular expressions match paths and queries

Java:

urlMatching("/your/([a-z]*)\\?and=query")

Response JSON

{
  "request": {
    "urlPattern": "/your/([a-z]*)\\?and=query"
    ...
  },
  ...
}

Keywords: Programming JSON Java emulator Web Server

Added by markmuir on Sat, 09 Nov 2019 20:50:17 +0200