soapui mock数据测试

  1. 下载soapui工具并安装
    https://www.soapui.org/downloads/latest-release.html

  2. 打开工具,新建“工程”-“Mock server”
    (1)右击选择“Create Empty Project”创建工程
    这里写图片描述

    (2)右击选择“New REST Mock Server
    这里写图片描述

  3. 选择MockServer,右击选择“Add new mock action”,选择接口服务的方法和URL

  4. 选添加的动作action,右击选择“New MockResponse”,输入名称为“test1”
    这里写图片描述

  5. 添加请求头信息和返回json串
    这里写图片描述

  6. 设置mockserver访问端口和地址,并启动mock,访问mock的接口,模拟终端使用的数据
    这里写图片描述

  7. 在soapui中调用mock的接口,访问http://192.168.1.106:8080/yst-test/findTabbar.json,返回的是mock的返回数据
    这里写图片描述

  8. mock接口通过请求参数返回不同的结果,通过script来实现
    (1)创建多个mock response,不同的返回值
    (2)编辑response返回个性值的script脚本,跟请求参数tabbarId的值返回相应的response

// Script dispatcher is used to select a response based on the incoming request.
// Here are few examples showing how to match based on path, query param, header and body

// Match based on path
def requestPath = mockRequest.getPath()
log.info "Path: "+ requestPath

if( requestPath.contains("json1") )
{
    // return the name of the response you want to dispatch
    return "JSON Response"
}


// Match based on query parameter
def queryString = mockRequest.getRequest().getQueryString()
log.info "QueryString: " + queryString

if( queryString.contains("tabbarId=1") )
{
    // return the name of the response you want to dispatch
    return "test2"
}
else if( queryString.contains("tabbarId=2") )
{
    // return the name of the response you want to dispatch
    return "test1"
}


// Match based on header
def acceptEncodingHeaderList = mockRequest.getRequestHeaders().get("Accept-Encoding")
log.info "AcceptEncoding Header List: " + acceptEncodingHeaderList

if( acceptEncodingHeaderList.contains("gzip,deflate") )
{
    // return the name of the response you want to dispatch
    return "GZiped Response"
}


// Match based on body
def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody

if( requestBody.contains("some data") )
{
    // return the name of the response you want to dispatch
    return "Response N"
}
  1. 如果实在本地运行mock服务且mock的值存在中文,接口访问值容易出现乱码,可部署在tomcat的服务器中访问
    这里写图片描述
    这里写图片描述

  2. 导出到war包,并放入tomcat运行
    这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/czy3y/article/details/72773563