Groovy Unit Test Framework spock Data Driven Demo

Keywords: Java Linux Lambda Junit

spock is an all-round unit testing framework.

The last article shared the use of the basic features of the spock framework, and on that basis, I made some attempts based on Groovy encapsulation, data-driven, and some advanced Groovy syntax I wrote.There's still a bit of a problem, I don't know if it's because I'm a Java and Groovy hybrid project, but there's also a solution.

Share the code for your reference:

package com.FunTester.spock.pratice

import com.fun.config.PropertyUtils
import com.fun.frame.SourceCode
import org.slf4j.Logger
import spock.lang.Shared
import spock.lang.Specification

import static com.fun.config.Constant.EMPTY
import static com.fun.config.Constant.getLongFile
import static com.fun.frame.Output.output
import static com.fun.frame.SourceCode.*

class Test02 extends Specification {

    @Shared
    def properties = PropertyUtils.getLocalProperties(getLongFile("1"))

    @Shared
    def cc = Arrays.asList(properties.getArrays("c")).stream().map {x -> Integer.valueOf(x)}.collect() as List

    @Shared
    def bb = Arrays.asList(properties.getArrays("b")).stream().map {x -> Integer.valueOf(x)}.collect() as List

    @Shared
    def aa = Arrays.asList(properties.getArrays("a")).stream().map {x -> Integer.valueOf(x)}.collect() as List

    @Shared
    Logger logger = getLogger(Test02.class.getName())

    def setup() {
        logger.info("Test method started")
    }

    def cleanup() {
        logger.info("Test method finished")
    }

    def setupSpec() {
        logger.info("Test Class[${getClass().getName()}]Here we go")
    }

    def cleanupSpec() {
        logger.info("Test Class[${getClass().getName()}]It's over")
    }

    def "Test Data Driven Demo"() {
        given:
        int c = 0

        expect:
        10 == a + b + c

        where:
        a | b
        1 | 9
        8 | 2
    }

    def "Test Data Read-Write Complete Data Driven"() {
        given:
        def a = 0
        def arrays = properties.getArrays("id")
        def s1 = properties.getPropertyInt("size1")
        def s2 = properties.getPropertyInt("size2")
        def list = Arrays.asList(arrays).stream().filter {x -> x.length() > 1}.collect() as List

        expect:
        s1 == arrays.size()
        s2 == list.size()
    }

    def "Test custom object actions"() {
        given: "Give a custom object"
        def per = new Per()
        per.age = 23
        per.name = "FunTester"
        def a = per

        expect:
        a.name == "FunTester"

    }

    def "Thread Security Test"() {
        given: "Multithreaded Support Testing,The effect is obvious here when the number of threads is changed to a large number"
        range(2).forEach {new Per().start()}
        sleep(1000)
        output(Per.i)
        expect:
        4 == Per.i
    }

    def "Test Set Validation Using Data Driven"() {
        given: "Written here cannot be where Use"

        expect:
        c * c == a * a + b * b

        where:
        c << cc
        b << bb
        a << aa
    }

    def "Test Array 0..10 Is Mode Available"() {
        expect:
        true == SourceCode.isNumber(x + EMPTY)

        where: "Brackets are required,Otherwise an error will be made"
        x << (0..5)

    }

    def "test lambda Is Writing Available"() {
        given:
        def collect =  range(10).filter {x -> x % 2 == 1}.collect() as List

        expect:
        collect.size() == 5
        collect.contains(3)
        collect.contains(5)
    }


/**
 * Test Class
 */
    class Per extends Thread {

        static int i

        @Override
        public void run() {
            i++
            sleep(100)
            i++
        }

        Per() {
        }

        Per(String name, int age) {
            this()
            this.name = name
            this.age = age
        }

        String name

        int age

        static def getAll() {
            i
        }

    }

}

Next time, I'll do some code demonstrations on spock s for my own tool classes and encapsulated request objects, and I'd like to welcome your attention.

Selected Technical Articles

Selected non-technical articles

Posted by MLJJ on Sun, 17 Nov 2019 00:01:42 -0800