[SoapUI] 重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对

重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对

封装的脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 log
     
    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 //Defaultly compare 5 decimal places
 
    public LooseyJSONComparator(JSONCompareMode mode) {
        super(mode)
    }
 
    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())
        }
    }
 
    @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)
        }
    }
 
    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里面如此调用

1
2
3
4
5
6
7
8
9
10
11
package direct
import org.json.*
 
def currentStepIndex = context.currentStepIndex
def previousStepName = testRunner.testCase.getTestStepAt(currentStepIndex-1).name
def prePreStepName = testRunner.testCase.getTestStepAt(currentStepIndex-2).name
 
def expectedJsonResponse =new JSONObject("{\"long-rescale\":\"1.519719\",\"short\":\"0.105816\",\"net\":\"1.477390\",\"long\":\"1.583196\"}")
def actualJsonResponse = new JSONObject("{\"long-rescale\":\"1.519715\",\"short\":\"0.105806\",\"net\":\"1.477390\",\"long\":\"1.583196\"}")
 
LooseyJSONComparator.assertEquals( expectedJsonResponse, actualJsonResponse)

  

输出结果:

 如果是通过获取test step的response来比对,脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package direct
import org.json.*
 
def currentStepIndex = context.currentStepIndex
def previousStepName = testRunner.testCase.getTestStepAt(currentStepIndex-1).name
def prePreStepName = testRunner.testCase.getTestStepAt(currentStepIndex-2).name
 
try{
    def expectedJsonResponse = new JSONObject(context.expand( '${'+prePreStepName+'#Response}' ))
    def actualJsonResponse = new JSONObject(context.expand( '${'+previousStepName+'#Response}' ))
    LooseyJSONComparator.assertEquals( expectedJsonResponse, actualJsonResponse)
}catch(JSONException e){
    assert false,"HTTP Response returns wrong, please have a check"
}

  

posted on   张缤分  阅读(1344)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示