[SoapUI] 重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对,在错误信息中打印当前循环的case number及其他附加信息
重载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及其他附加信息的更多相关文章
- PHP 将json的int类型转换为string类型 解决php bigint转科学计数法的问题
/** * 将json的int类型转换为string类型 * @param $str * @param int $minLength 最小的转换位数,即只有大于等于这个长度的数字才会被转换为字符串 * ...
- [SoapUI] 将科学计数法转化为普通数字,并且只保留小数点后几位
方案一: import java.text.NumberFormat class CompareHashMap { def regEx_Numeric = '-?[1-9]\\d*$|-?([1-9] ...
- [SoapUI] 通过JSONAssert比较两个环境的JSON Response,定制化错误信息到Excel
package ScriptLibrary; import org.json.JSONArray; import org.json.JSONException; import org.json.JSO ...
- DataTables学习:从最基本的入门静态页面,使用ajax调用Json本地数据源实现前端开发深入学习,根据后台数据接口替换掉本地的json本地数据,以及报错的处理地方,8个例子(显示行附加信息,回调使用api,动态显示和隐藏列...),详细教程
一.DataTables 个人觉得学习一门新的插件或者技术时候,官方文档是最根本的,入门最快的地方,但是有时候看完官方文档,一步步的动手写例子,总会出现各种莫名其妙的错误,需要我们很好的进行研究出错 ...
- 【SoapUI】比较Json response
package direct; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject ...
- [SoapUI] 比较JSON Response
比较两个JSON, ID是数字时,处理成统一的格式:只保留小数点后5位 package direct; import org.json.JSONArray; import org.json.JSONE ...
- Angularjs+node+Mysql实现地图上特定点的定位以及附加信息展示
注:本博文为博主原创,转载请注明出处. 在上一篇博文中主要讲述了如何利用AngularJs+Node+MySql构建项目,并实现地图上的多点标注,今天在这篇文章中,我们将在上一个项目的基础上,实现特定 ...
- 使用Groovy处理SoapUI中Json response
最近工作中,处理最多的就是xml和json类型response,在SoapUI中request里面直接添加assertion处理json response的话,可以采用以下方式: import gro ...
- Json Serialize 忽略特定属性
Json Serialize 忽略特定属性 Json Serialize SerializeFilter 忽略特定属性 key words:Json Serialize jackson fastjso ...
随机推荐
- flask中的蓝图与红图
内容: 1.flask中的蓝图 2.flask子域名实现 3.flask中的红图 1.flask中的蓝图 一个大型项目中视图比较多,如果仅仅是写在app.py中不方便管理,蓝图就可以做到分功能分目录结 ...
- 阻止form提交数据,通过ajax等上传数据
btn.click(function (event) { event.preventDefault(); // 组织发送 $.ajax({ ...}) })
- ajax调用json
//var data_str='({"detail":[{"html":"科技科技科技有限公司"},{"html":&q ...
- leetcode944
public class Solution { public int MinDeletionSize(string[] A) { ; ; j < A[].Length; j++) { ; i & ...
- leetcode541
public class Solution { public string ReverseStr(string s, int k) { var len = s.Length; //记录k的倍数 //分 ...
- EOF与子过程返回
在2000及其以上系统,P处理语句GOTO新增了:EOF系统标签,意思是移动到当前P处理文件的结尾,EOF==END OF FILE的缩写,意为文件结尾,主要表现形式为:GOTO :EOFOR ...
- Spring MVC 异常处理 - SimpleMappingExceptionResolver
希望对一些异常统一处理,他将异常类名映射为视图名,即发生异常时使用对应的视图报告异常.
- WDA-文档-基础篇/进阶篇/讨论篇
本文介绍SAP官方Dynpro开发文档NET310,以及资深开发顾问编写的完整教程. 链接:http://pan.baidu.com/s/1eR9axpg 密码:kf5m NET310 ABAP ...
- Axure8 实现移动端页面上下滑动效果
目前,很多Axure新人都在问如何实现界面上下滑动效果,网上相关的教程也不少,各有各的方法,但是很少有教程对滑动界限设置做出比较详细的说明,其实在工作过程中,个人发现练好Axure是很有意提升逼格的, ...
- 查询sql执行速度
with QS as( select cp.objtype as object_type ,db_name(st.dbid)as [database] ...