重载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. python拓展1 week1-week5复习回顾

    知识内容: 1.python基础概念及基础语法 2.python基础数据类型 3.python模块相关 4.python函数相关 5.python面向对象相关 6.python文件处理相关 注:本节内 ...

  2. UVA699-落叶-二叉树

    1:用一根数轴,根结点是坐标index,左结点-1,右结点+1 还有一个0ms的不知道怎么过的 #include<stdio.h> #include<iostream> #in ...

  3. ldap复制

    1.使用yum命令安装openldap,openldap-servers,openldap-clients $ yum install openldap $ yum install openldap- ...

  4. Some facts about topological sort

    Definition: a topological sort of a DAG G is a sort such that for all edge (i,j) in G, i precedes j. ...

  5. 自定义BeanUtils

    package com.icil.booking.service.util; import java.math.BigDecimal; import java.text.ParseException; ...

  6. react部署之页面空白

    react部署之页面空白 问题:create-react-app build打包后,页面出现空白. 可能一: 控制台报错,js等文件找不到(404) 文件路径问题,只需修改package.json文件 ...

  7. apache配置修改

    1.如何设置请求等待时间   在httpd.conf里面设置:   TimeOut n   其中n为整数,单位是秒.     2. 3.如何使得apache监听在特定的端口   修改httpd.con ...

  8. IIS ashx

    win2008 IIS ashx http://127.0.0.1:801/testHandler.ashx 在服务器上用IE打开提示 HTTP 错误 404.17 - Not Found 请求的内容 ...

  9. Java Collection.sort 排序升序, 降序问题

    不多说,记住2点, 直接上代码(下面是降序): package mall; import java.util.ArrayList; import java.util.Collections; impo ...

  10. import tensorflow 报错,CentOS 升级 glibc

    问题描述: ]: import tensorflow as tf ImportError: /lib64/libc.so.: version `GLIBC_2.' not found (require ...