浅谈 Mock Server

article/2025/9/18 4:17:20

What?什么是 Mock Server

Mock 是模拟的意思。在测试中,通常表述为:对测试过程中不容易构造或者不容易获取的物件,用一个虚拟的物件来进行模拟的一个过程。能够提供 Mock 功能的服务就叫作 Mock Server。 Mock Server 通过模拟真实的服务,提供对来自客户端请求的真实响应。

Why?为什么要用 Mock Server

  1. 在开发初期先模拟外部接口,避免直接使用实际外部接口进行开发调试,提高开发效率
  2. 全面准确的模拟外部接口的返回,提高自身代码的可靠性,提升验证的全面性

How?怎么用 Mock Server

主要包括两种使用方式,一种是通过一个现成的 Mock Server 服务,这种的好处是对代码无侵入,只需要将调用的 URL 修改为 Mock Server 提供的 URL 即可。另一种是在代码里面显式的编写相关的规则,这样的好处是不再需要单独启用服务,在代码里面就可以完成 Mock,主要在单测的时候使用。

使用方式一:通过现成的服务打桩

可以使用类似 apifox 工具本地/线上打桩,apifox 不仅有基于 Swgger 的 API 管理的能力,同时也具备 API Mock 的功能。具体使用方法可以访问: https://www.apifox.cn/

使用方式二:通过代码打桩

MockServer 官方文档 http://www.mock-server.com 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 {@Rulepublic MockServerRule server = new MockServerRule(this, 1080);@Testpublic 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));}
}

http://chatgpt.dhexx.cn/article/crRWN6nD.shtml

相关文章

Mock Server 入门

Mock Server介绍 什么是mock ? 我在去年的时候介绍一篇幅 python mock的基本使用&#xff0c;http://www.cnblogs.com/fnng/p/5648247.html 主要是针对单元测试的应用&#xff0c;它可以很方便的解除单元测试中各种依赖&#xff0c;大大的降低了编写单元测试的难度。 什么是Mo…

Matlab解决脚本中中文乱码问题

首先下载字体yahei consolas hybrid。百度网盘地址&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1Zv2FMt3uow7DZix2rtAbrA 提取码&#xff1a;am4d 然后将下载的字体保存在C:\Windows\Fonts中&#xff0c;启动Matlab2018a这里使用的是2018a版本&#xff0c;其他版…

WEB UI自动化测试之AutoMagic自动化测试

AutoMagic自动化测试平台 AutoMagic 是一个基于WebUI的自动化管理平台。为什么叫AutoMagic呢&#xff1f;因为自动化&#xff08;Automation&#xff09;在执行起来的时候是一个很神奇的事情&#xff0c;它可以无人值守的模拟人的操作&#xff0c;就像魔术&#xff08;Magic&a…

Jupyter Notebook从入门到精通

下载 本课程练习完整代码 Jupyter Notebook&#xff1a; https://gist.github.com/zgpeace/8d3eb8c803a54d1ca797fa26cb68bd4c 财富500强 csv下载 https://github.com/zgpeace/fortune500.git 1. 什么是 Jupyter 笔记本&#xff1f; Jupyter Notebook 是一个非常强大的工具&…

dev shm mysql_新特性:/dev/shm对Oracle 11g的影响

ORACLE 从11g版本开始&#xff0c;引入了一个自动内存管理(Automatic Memory Management)特性&#xff0c;该特性需要更多的共享内存(/dev/shm)&#xff0c;因此如 /dev/shm对Oracle 11g的影响: ORACLE 从11g版本开始&#xff0c;引入了一个自动内存管理(Automatic Memory Mana…

Git分布式版本控制工具

Git分布式版本控制工具 文章目录 Git分布式版本控制工具1、目标 2、概述2.1、开发中的实际场景2.2、版本控制器的方式2.3、Git工作流程图 3、Git安装与常用命令3.1、Git 环境配置3.1.1 下载与安装3.1.2 基本配置3.1.3 为常用指令配置别名3.1.4 解决 GitBash 乱码问题 3.2、获取…

关于我在windows使用volatility取证这档事

官网下载地址&#xff1a;https://www.volatilityfoundation.org/releases volatility3的官方文档&#xff1a;https://volatility3.readthedocs.io/en/latest/basics.html 下载 看清有两个版本&#xff0c;用法不一样 第一次我下载了Volatility 2.6 Windows Standalone Exe…

DLNLP学习笔记03(Speech Recognition: LAS模型)

Listen, Attend, and Spell (LAS) [Chorowski. et al., NIPS’15] 论文地址&#xff1a;https://arxiv.org/pdf/1508.01211.pdf 1 模型简介&#xff1a; 2 Listen&#xff1a;其中Encoder可以为RNN&#xff0c;CNN或者RNNCNN。 Listen-down sampling&#xff1a;&#xff08;RN…

On Device Debug!IDA+GDB trace automagic.apk in s1

2019独角兽企业重金招聘Python工程师标准>>> http://forum.xda-developers.com/showthread.php?t2050393 Well... I have attached a debugger to native code, set breakpoints, analyzed registers, memory, etc. It wasnt that easy though. It took me severa…

【Jetson Nano 入门】环境配置汇总

文章目录 前言一、镜像烧写二、PWM风扇自动调速三、使用MicroUSB在电脑打开终端四、VNC实现局域网及网线直连通信五、SSH文件传输六、状态监控软件Jtop七、检查CUDA、OpenCV及cuDNN八、USB摄像头测试软件Camorama九、CSI摄像头测试十、实现Yolov4-tiny的USB摄像头实时检测 前言…

WEB UI自动化测试之AutoMagic自动化测试平台开源

作者介绍&#xff1a; 网名: Ray 介绍&#xff1a;笑起来像个孩子&#xff0c;冷起来是个迷。 博客&#xff1a;http://www.cnblogs.com/tsbc/ 2018年3月29日&#xff0c;Ray说准备把AutoMagic自动化测试管理平台开源了&#xff01;&#xff01;&#xff01; 这是个好消息&…

三星s20 android auto,Automagic一个更简单的方式来自动化您的Android手机 | MOS86

你有没有想过你的智能手机会在你回家的时候开始播放音乐&#xff1f;当你关闭社交网络的时候怎么样&#xff1f;Automagic是一种替代方案&#xff0c;通过使用易于理解的流程图执行许多相同的任务&#xff0c;向用户提供了一种更简单的自动化Android手机的方法。 Automagic如何…

AutoMagic自动化测试平台简介

PS:给想做自动化平台同学一点思路。 AutoMagic 是一个基于WebUI的自动化管理平台。为什么叫AutoMagic呢&#xff1f;因为自动化&#xff08;Automation&#xff09;在执行起来的时候是一个很神奇的事情&#xff0c;它可以无人值守的模拟人的操作&#xff0c;就像魔术&#xff0…

AutoMagic设计思路简介及新增自定义关键字实例

目录 简介 AutoMagic介绍 SeleniumKey介绍 自定义关键字 简介 AutoMagic 是一个基于WebUI的自动化管理平台。为什么叫AutoMagic呢&#xff1f;因为自动化在执行起来的时候是一个很神奇的事情&#xff0c;它可以无人值守的模拟人的操作&#xff0c;就像魔术&#xff08;Magic&am…

AutoMagic-开源自动化平台的容器化构建

github上看到AutoMagic自动化平台开源了&#xff0c;一时手痒&#xff0c;就试着搭了一套环境&#xff0c;现在把思路和大家说一说。 AutoMagic从其工作分工分两部分&#xff1a; 1&#xff1a;Web端管理平台 管理平台基于Python Django框架Mysql开发&#xff0c;主要用来管理自…

Automagic Premium 安卓上的自动化神器

每当我使用手机时&#xff0c;就对各种App的签到领奖非常反感&#xff0c;如果不签好像吃了亏&#xff0c;签一次花的时间自己也觉得不值。这时候我就在想&#xff0c;如果有一个App&#xff0c;能够自动帮我完成这种每天重复执行的任务就好了。之前找到一个叫uiautomator2的py…

[重磅消息]AutoMagic自动化测试平台开源

作者介绍&#xff1a; 网名: Ray 介绍&#xff1a;笑起来像个孩子&#xff0c;冷起来是个迷。 博客&#xff1a;http://www.cnblogs.com/tsbc/ 2018年3月29日&#xff0c;Ray跟我说准备把AutoMagic自动化测试管理平台开源了&#xff01;&#xff01;&#xff01; 这是个好消息&…

AutoMagic使用说明

简介 自动化测试管理平台&#xff0c;可以进行用户、产品、项目、模块、测试用例、测试元素、测试关键字、测试报告的管理等。 Github地址&#xff1a;https://github.com/radiateboy/automagic/ 用户和产品管理 本平台是自动化管理平台&#xff0c;整个平台结构是 用户>产品…

AutoMagic使用说明以及图文操作步骤

一、简介 自动化测试管理平台&#xff0c;可以进行用户、产品、项目、模块、测试用例、测试元素、测试关键字、测试报告的管理等。 二、平台结构设计 三、用例关系 四、用户和产品管理 本平台是自动化管理平台&#xff0c;整个平台结构是 用户>产品>项目>模块>…

automagic 使用_使用Automagic在Android设备上自动化任务

automagic 使用 Automation is always a good thing, and we’ve looked at various ways in which you can automate tasks on your Android. AutoMagic takes a different approach to things, making it possible to create flow charts that are used to trigger actions b…