Last article talked about JsonPath tool class encapsulation, left a hole, is about the tool class unit test, due to noon free, so use the unit test framework Spock to write a little unit test cases, share, for everyone’s reference.

  • useGroovyLanguage,spockTest framework, for more information please refer to the article:
  • Spock is configured in Maven and Gradle
  • Groovy unit testing framework Spock basic functionality Demo
  • Groovy unit testing framework Spock data driven Demo
  • Life is short? Try Groovy for unit testing
  • Initial exploration of Spock 2.0 M1
  • Unit testing frameworks spock and Mockito applications
  • In the middleGroovyText block, interested can look atJava text block.

Unit test cases

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 "Test class begin! ${logger.getName()}"
    }

    def setup() {
        logger.info ${times.get()} ${times.get()}
    }

    def cleanup() {
        logger.info ${times.getAndIncrement()} end
    }

    def cleanupSpec() {
        logger.info "End of test class! ${logger.getName()}"
    }

    def "Verify the value effect"() {

        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 "Validate array related functions"() {
        expect:
        value as String == json.getString(path)

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

    def "Validate functions that handle arrays"() {
        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()"}}Copy the code
  • In the last use case, I deliberately left a BUG, that is, when calculating the standard deviation, I omitted the following digits, which led to the failure of a use case.

Console output

  • Only the output of the last method is included here, the others are all successful, so it is omitted.
INFO->The 16th test is over!
INFO->The 16th test is over!
INFO->The 17th test is over!
INFO->The 17th test is over!
INFO->The 18th test is over!
INFO->The 18th test is over!
INFO->The 19th test is over!
INFO->The 19th test is over!
INFO->The 20th test is over!
INFO->The 20th test is over!
INFO->The 21st test is over!
INFO->The 21st test is over!Condition not satisfied: Value = = json. The 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: Reflective 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]], 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. Validate functions that handle arrays (jsonutiltest.groovy :120)Copy the code

  • The public number FunTester first, original sharing enthusiasts, Tencent cloud and nuggets community home page recommendation, Zhihu seven level original author, more original articles, welcome to pay attention to, exchange, prohibit the third party to reprint without authorization.

Hot article selected

  • Function This interface is used to test albums
  • Open source testing service
  • Performance Testing Topics
  • Graphic HTTP brain map
  • Programming thinking for everyone
  • 2020 Tester self-improvement
  • Fiddler Everywhere is the future
  • Test development engineer work skills
  • Fiddler Everywhere answers questions
  • Selenium4 IDE, it’s finally here