MockServer 简单示例


如今的业务系统越来越复杂庞大,各个功能直接的调用也是多如牛毛,但如果在联调的时候,恰好被调的接口正在开发,怎么办?傻傻的等么,不存在的!这时会搭建一些server来进行mock。使得被开发功能的调试和测试功能能够正常进行下去。这就是mock server的出现的原因。

MockServer 官方文档 http://www.mock-server.com

mock server的作用

image.png

Maven 引入

<dependency>
    <groupId>org.mock-server</groupId>
    <artifactId>mockserver-netty</artifactId>
    <version>5.4.1</version>
</dependency>

<dependency>
    <groupId>org.mock-server</groupId>
    <artifactId>mockserver-client-java</artifactId>
    <version>5.4.1</version>
</dependency>

示例一

ClientAndServer 用法

package com.yzy.mock;

import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.Cookie;

import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static org.mockserver.model.Parameter.param;

public class MockServer {
    public static void main(String[] args) {
        ClientAndServer server = new ClientAndServer(1080);
        server.when(
                request()
                        .withMethod("GET")
                        .withPath("/test")
                        .withQueryStringParameters(
                                param("p", "1")
                        )
        ).respond(
                response()
                        .withCookie(new Cookie("cKey", "cValue"))
                        .withBody("test1")
        );

        server.when(
                request()
                        .withMethod("GET")
                        .withPath("/test")
                        .withQueryStringParameters(
                                param("p", "2")
                        )
        ).respond(
                response()
                        .withBody("test2")
        );
    }
}

启动main方法,打开浏览器,输入http://localhost:1080/test?p=1,页面返回test1,并且成功写入cookie。注意写入的cookie 是Session 级别的,会话关闭将消失。输入http://localhost:1080/test?p=2,页面返回test2,说明MockServer 同时监听的两个匹配都有效。

示例二

MockServerClient 用法

package mock;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Rule;
import org.junit.Test;
import org.mockserver.client.MockServerClient;
import org.mockserver.junit.MockServerRule;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

public class MockServerTest {
    @Rule
    public MockServerRule server = new MockServerRule(this, 1080);

    @Test
    public void test() throws IOException {
        MockServerClient mockClient = new MockServerClient("localhost", 1080);
        String expected = "hello world";

        mockClient.when(
                request()
                        .withPath("/hello")
                        .withMethod("GET")
        ).respond(
                response()
                        .withStatusCode(200)
                        .withBody(expected)
        );

        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://localhost:1080/hello");
        CloseableHttpResponse response = client.execute(httpGet);

        //验证
        InputStream content = response.getEntity().getContent();
        InputStreamReader inputStreamReader = new InputStreamReader(content);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String responseText = bufferedReader.readLine();
        assertThat(responseText, equalTo(expected));
    }
}

参考链接:

https://www.jianshu.com/p/ea0ebf93cb8f

https://blog.csdn.net/xu547823501/article/details/86076188

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×