soapui mock数据测试

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

  2. 打开工具,新建“工程”-“Mock server”
    (1)右击选择“Create Empty Project”创建工程
    soapui mock数据测试_第1张图片

    (2)右击选择“New REST Mock Server
    soapui mock数据测试_第2张图片

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

  4. 选添加的动作action,右击选择“New MockResponse”,输入名称为“test1”
    soapui mock数据测试_第3张图片

  5. 添加请求头信息和返回json串
    soapui mock数据测试_第4张图片

  6. 设置mockserver访问端口和地址,并启动mock,访问mock的接口,模拟终端使用的数据
    soapui mock数据测试_第5张图片

  7. 在soapui中调用mock的接口,访问http://192.168.1.106:8080/yst-test/findTabbar.json,返回的是mock的返回数据
    soapui mock数据测试_第6张图片

  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的服务器中访问
    soapui mock数据测试_第7张图片
    soapui mock数据测试_第8张图片

  2. 导出到war包,并放入tomcat运行
    soapui mock数据测试_第9张图片

soapui mock数据测试_第10张图片

你可能感兴趣的:(测试技术)