JsonPath工具类单元测试

发表于:2020-9-29 09:53

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:FunTester    来源:博客园

  上期文章讲到JsonPath工具类封装,遗留了一个坑,就是关于工具类的单元测试,由于中午得空,所以使用单元测试框架Spock写了一点点单元测试用例,分享出来,供大家参考。
  单元测试用例
package com.FunTester.spock.utils_test

import com.alibaba.fastjson.JSON
import com.fun.utils.JsonUtil
import org.slf4j.Logger
import spock.lang.Shared
import spock.lang.Specification

import java.util.concurrent.atomic.AtomicInteger

import static com.fun.frame.SourceCode.getLogger

public class JsonUtilTest extends Specification {

    @Shared
    Logger logger = getLogger(this.getClass().getName())

    @Shared
    AtomicInteger times = new AtomicInteger(1)

    @Shared
    JsonUtil json = JsonUtil.getInstance(JSON.parseObject("{" +
            "    \"store\": {" +
            "        \"book\": [" +
            "            {" +
            "                \"category\": \"reference\"," +
            "                \"author\": \"Nigel Rees\"," +
            "                \"title\": \"Sayings of the Century\"," +
            "                \"page\": \"D\"," +
            "                \"pages\": [\"S\",\"X\",\"G\"]," +
            "                \"price\": 8.95" +
            "            }," +
            "            {" +
            "                \"category\": \"fiction\"," +
            "                \"author\": \"Evelyn Waugh\"," +
            "                \"title\": \"Sword of Honour\"," +
            "                \"page\": \"A\"," +
            "                \"pages\": [\"A\",\"B\"]," +
            "                \"price\": 12.99" +
            "            }," +
            "            {" +
            "                \"category\": \"fiction\"," +
            "                \"author\": \"Herman Melville\"," +
            "                \"title\": \"Moby Dick\"," +
            "                \"isbn\": \"0-553-21311-3\"," +
            "                \"page\": \"B\"," +
            "                \"pages\": [\"E\",\"F\"]," +
            "                \"price\": 8.99" +
            "            }," +
            "            {" +
            "                \"category\": \"fiction\"," +
            "                \"author\": \"J. R. R. Tolkien\"," +
            "                \"title\": \"The Lord of the Rings\"," +
            "                \"isbn\": \"0-395-19395-8\"," +
            "                \"page\": \"C\"," +
            "                \"pages\": [\"C\",\"D\"]," +
            "                \"price\": 22.99" +
            "            }" +
            "        ]," +
            "        \"bicycle\": {" +
            "            \"color\": \"red\"," +
            "            \"price\": 19.95" +
            "        }" +
            "    }," +
            "    \"expensive\": 10," +
            "    \"ss\": [32,32,4,23]" +
            "}"))

    def setupSpec() {
        logger.info "测试类开始! ${logger.getName()}"
    }

    def setup() {
        logger.info "第 ${times.get()} 次测试结束!"
    }

    def cleanup() {
        logger.info "第 ${times.getAndIncrement()} 次测试结束!"
    }

    def cleanupSpec() {
        logger.info "测试类结束! ${logger.getName()}"
    }

    def "验证取值效果"() {

        expect:
        value as String == json.getString(path)

        where:
        value                           | path
        10                              | "\$.expensive"
        "Sword of Honour"               | "\$.store.book[1].title"
        "0-395-19395-8"                 | "\$.store.book[3].isbn"
        19.95                           | "\$.store.bicycle.price"
        "[19.95,8.95,12.99,8.99,22.99]" | "\$..price"
        "[]"                            | "\$..fdsss"
        ""                              | "\$.fdsss"

    }

    def "验证数组相关功能"() {
        expect:
        value as String == json.getString(path)

        where:
        value           | path
        """["S","X"]""" | "\$..book[0].pages[0,1]"
        """["G"]"""     | "\$..book[0].pages[-1]"
        """["C"]"""     | "\$..book[?(@.price == 22.99)].page"
        """["C"]"""     | "\$..book[?(@.price in [22.99])].page"
        """["D"]"""     | "\$..book[?(@.price nin [22.99,8.99,12.99])].page"
        """["C"]"""     | "\$..book[?(@.title =~ /.*Lord.*/)].page"
        """["D","C"]""" | "\$..book[?(@.title =~ /.*the.*/)].page"
        """["B","C"]""" | "\$..book[?(@.pages subsetof ['D','C','E','F'])].page"
    }

    def "验证处理数组的函数"() {
        expect:
        value == json.getDouble(path)

        where:
        value   | path
        91      | "\$.ss.sum()"
        4       | "\$.ss.min()"
        32      | "\$.ss.max()"
        22.75   | "\$.ss.avg()"
        11.4318 | "\$.ss.stddev()"
        4       | "\$.ss.length()"

    }

}
  最后一个用例里面,我特意留了一个BUG,就是在计算标准差的时候,我省去了后面的几位数字,导致一个用例失败。
  控制台输出
INFO-> 第 16 次测试结束!
INFO-> 第 16 次测试结束!
INFO-> 第 17 次测试结束!
INFO-> 第 17 次测试结束!
INFO-> 第 18 次测试结束!
INFO-> 第 18 次测试结束!
INFO-> 第 19 次测试结束!
INFO-> 第 19 次测试结束!
INFO-> 第 20 次测试结束!
INFO-> 第 20 次测试结束!
INFO-> 第 21 次测试结束!
INFO-> 第 21 次测试结束!

Condition not satisfied:

value == json.getDouble(path)
|     |  |    |         |
|     |  |    |         $.ss.stddev()
|     |  |    11.431863365173676
|     |  <com.fun.utils.JsonUtil@192d74fb json=[ss:[32, 32, 4, 23], store:[bicycle:[color:red, price:19.95], book:[[pages:[S, X, G], author:Nigel Rees, price:8.95, page:D, category:reference, title:Sayings of the Century], [pages:[A, B], author:Evelyn Waugh, price:12.99, page:A, category:fiction, title:Sword of Honour], [pages:[E, F], author:Herman Melville, price:8.99, isbn:0-553-21311-3, page:B, category:fiction, title:Moby Dick], [pages:[C, D], author:J. R. R. Tolkien, price:22.99, isbn:0-395-19395-8, page:C, category:fiction, title:The Lord of the Rings]]], expensive:10]>
|     false
11.4318

<Click to see difference>


at com.FunTester.spock.utils_test.JsonUtilTest.验证处理数组的函数(JsonUtilTest.groovy:120)
  这里只放了最后一个方法的输出,其他的都是成功的,所以就省去了。

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号