重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对,在错误信息中打印当前循环的case number及其他附加信息

package direct

import org.skyscreamer.jsonassert.*
import org.skyscreamer.jsonassert.comparator.*
import org.json.*
import net.sf.json.JSONException
import java.text.NumberFormat public class LooseyJSONComparator extends DefaultComparator {
def extraInfo def regEx_Numeric = '-?[1-9]\\d*$|-?([1-9]\\d*\\.\\d*|0\\.\\d*|0?\\.0+|0)$' //匹配内容为数字的字符串
def regEx_ScientificNotation = '^((-?\\d+.?\\d*)[Ee]{1}(-?\\d+))$' //科学计数法正则表达式 int decimalPrecision = 5 //默认精度为小数点后5位 public LooseyJSONComparator(JSONCompareMode mode) {
super(mode)
} //支持错误日志输出附加信息,譬如循环时的case number,请求的requestID等
public LooseyJSONComparator(JSONCompareMode mode,def extraInfo) {
super(mode)
this.extraInfo = extraInfo
} public static void assertEquals( Object expected, Object actual) throws JSONException {
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, new LooseyJSONComparator(JSONCompareMode.LENIENT))
if (result.failed()) {
throw new AssertionError(result.getMessage())
}
} public static void assertEquals( Object expected, Object actual, def extraInfo) throws JSONException {
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, new LooseyJSONComparator(JSONCompareMode.LENIENT))
if (result.failed()) {
throw new AssertionError(extraInfo+result.getMessage())
}
} @Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
throws JSONException {
if (expectedValue instanceof String && actualValue instanceof String) {
def expectedValueTemp=formatDecimalPrecision(expectedValue)
def actualValueTemp=formatDecimalPrecision(actualValue)
if (expectedValueTemp!=actualValueTemp){
result.fail(prefix, expectedValue, actualValue)
}
} else if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) {
if (expectedValue instanceof JSONArray) {
compareJSONArray(prefix, (JSONArray) expectedValue, (JSONArray) actualValue, result)
} else if (expectedValue instanceof JSONObject) {
compareJSON(prefix, (JSONObject) expectedValue, (JSONObject) actualValue, result)
} else if (!expectedValue.equals(actualValue)) {
result.fail(prefix, expectedValue, actualValue)
}
} else {
result.fail(prefix, expectedValue, actualValue)
}
} //只精确比较小数点后5位
def formatDecimalPrecision(def dataValue){
NumberFormat format = NumberFormat.getNumberInstance()
format.setMaximumFractionDigits(decimalPrecision) dataValue = dataValue.toString() //将科学计数法转换为普通数字之后再处理精确度
if(dataValue.matches(regEx_ScientificNotation)){
BigDecimal db = new BigDecimal(dataValue)
dataValue = db.toPlainString()
} //确定字符串的内容是否为数字
if(dataValue.matches(regEx_Numeric)){
dataValue = Double.parseDouble(dataValue)
dataValue = format.format(dataValue)
}
return dataValue
}
}

SoapUI里面如此调用:

package direct
import org.json.* String caseNum = context.expand('${DataSource#caseNumber}')
String extraInfo = caseNum+ ", " def currentStepIndex = context.currentStepIndex
def previousStep = testRunner.testCase.getTestStepAt(currentStepIndex-1)
def prePreStep = testRunner.testCase.getTestStepAt(currentStepIndex-2) def previousStepName = previousStep.name
def prePreStepName = prePreStep.name try{
def requestIdTest =previousStep.testRequest.messageExchange.requestHeaders.get("X-API-RequestId")
def requestIdBmk =prePreStep.testRequest.messageExchange.requestHeaders.get("X-API-RequestId")
extraInfo += "requestIdBmk = "+requestIdBmk+", requestIdTest = "+requestIdTest+", "
}
catch(NullPointerException e){
assert false,extraInfo+"HTTP Request is null"
} try{
def expectedJsonResponse = new JSONObject(context.expand( '${'+prePreStepName+'#Response}' ))
def actualJsonResponse = new JSONObject(context.expand( '${'+previousStepName+'#Response}' ))
LooseyJSONComparator.assertEquals( expectedJsonResponse, actualJsonResponse,extraInfo)
}
catch(JSONException e){
assert false,extraInfo+"HTTP Response is not JSON"
}

  

[SoapUI] 重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对,在错误信息中打印当前循环的case number及其他附加信息的更多相关文章

  1. PHP 将json的int类型转换为string类型 解决php bigint转科学计数法的问题

    /** * 将json的int类型转换为string类型 * @param $str * @param int $minLength 最小的转换位数,即只有大于等于这个长度的数字才会被转换为字符串 * ...

  2. [SoapUI] 将科学计数法转化为普通数字,并且只保留小数点后几位

    方案一: import java.text.NumberFormat class CompareHashMap { def regEx_Numeric = '-?[1-9]\\d*$|-?([1-9] ...

  3. [SoapUI] 通过JSONAssert比较两个环境的JSON Response,定制化错误信息到Excel

    package ScriptLibrary; import org.json.JSONArray; import org.json.JSONException; import org.json.JSO ...

  4. DataTables学习:从最基本的入门静态页面,使用ajax调用Json本地数据源实现前端开发深入学习,根据后台数据接口替换掉本地的json本地数据,以及报错的处理地方,8个例子(显示行附加信息,回调使用api,动态显示和隐藏列...),详细教程

    一.DataTables  个人觉得学习一门新的插件或者技术时候,官方文档是最根本的,入门最快的地方,但是有时候看完官方文档,一步步的动手写例子,总会出现各种莫名其妙的错误,需要我们很好的进行研究出错 ...

  5. 【SoapUI】比较Json response

    package direct; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject ...

  6. [SoapUI] 比较JSON Response

    比较两个JSON, ID是数字时,处理成统一的格式:只保留小数点后5位 package direct; import org.json.JSONArray; import org.json.JSONE ...

  7. Angularjs+node+Mysql实现地图上特定点的定位以及附加信息展示

    注:本博文为博主原创,转载请注明出处. 在上一篇博文中主要讲述了如何利用AngularJs+Node+MySql构建项目,并实现地图上的多点标注,今天在这篇文章中,我们将在上一个项目的基础上,实现特定 ...

  8. 使用Groovy处理SoapUI中Json response

    最近工作中,处理最多的就是xml和json类型response,在SoapUI中request里面直接添加assertion处理json response的话,可以采用以下方式: import gro ...

  9. Json Serialize 忽略特定属性

    Json Serialize 忽略特定属性 Json Serialize SerializeFilter 忽略特定属性 key words:Json Serialize jackson fastjso ...

随机推荐

  1. 通过cookie验证用户登录

    # cookie # cookie# 当你在浏览器登陆时,浏览器记录这个登录信息(服务器响应时发送请求的数据和登录信息),再次访问时 浏览器会将访问请求和缓存的登录信息都发送到服务器, # 服务器通过 ...

  2. python之匿名函数和递归函数

    递归函数 <1>什么是递归函数 通过前面的学习知道一个函数可以调用其他函数. 如果一个函数在内部不调用其它的函数,而是自己本身的话,这个函数就是递归函数. <2>递归函数的作用 ...

  3. etcd查看key-value

    get/set key-value etcdctl get/set /key-path etcdctl watch --recursive /test/sm/default/apps 查看所有key- ...

  4. CentOS7.3下yum练手安装Nginx

    安装Nginx # 查看相关信息 yum info nginx yum info httpd # 移除 httpd,也就是 Apache yum remove httpd -y # 安装 nginx ...

  5. 64位win10系统无法安装.Net framework3.5的两种解决方法

    参考网站: https://blog.csdn.net/zang141588761/article/details/52177290 在Windows10中,当我们安装某些软件的时候会提示“你的电脑上 ...

  6. spring boot 自定义异常

    1.创建一个异常: public class LdapQueryException extends Exception { private Integer code; private String m ...

  7. leetcode202

    public class Solution { private int SumSqares(int n) { //将一个数字的各个数位的值分开存储 var list = new List<int ...

  8. delphi 加密 XOR

    From  http://www.delphigeist.com/2009/09/text-encryption-with-xor.html Text encryption with XOR   Ev ...

  9. OpenCV版本下载

    https://sourceforge.net/projects/opencvlibrary/files/opencv-win/

  10. Mybatis知识(3)

    1.JDBC编程有哪些不足之处,MyBatis是如何解决这些问题的? ① 数据库链接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题. 解决:在SqlMapConfig ...