Spock

test

Posted by Static on March 26, 2020

what?

Spock 框架是一个基于groovy语法的测试框架,由于使用groovy,所以使用起来比junit更加灵活,测试用例的写法更加简单、通熟易懂,可利用mock多场景数据测试项目代码,保证代码质量。

Spock与Junit对比

Spock JUnit 说明
setup() @Before 在每个case执行之前都会run
cleanup() @After 在每个case执行完之后都会run
setupSpec() @BeforeClass 所有test方法执行之前执行
cleanupSpec() @AfterClass 在所有test方法执行完之后执行,比如:连接关闭,资源回收等操作

how?

1. 添加groovy和Spock依赖

        <dependency>
            <groupId>org.Spockframework</groupId>
            <artifactId>Spock-core</artifactId>
            <version>1.3-groovy-2.4</version>
            <scope>test</scope>
        </dependency>
        <!--与 spring 集成-->
        <dependency>
            <groupId>org.Spockframework</groupId>
            <artifactId>Spock-spring</artifactId>
            <version>1.3-groovy-2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.12</version>
            <scope>test</scope>
        </dependency>

2. 测试类继承 Specification

class BaseTest extends Specification {
    ...
}

3. 常用基本用法

  • assert
def "expect 断言() {
        expect:
        sum(1, 1) == 2
    }
  • setup
    def "setup given 启动测试"() {
        //也可以用given代替
        setup:
        
        def name = "test"

        println name // 打印
    }
  • when and then
    def "when then 配合使用,when赋值,then对结果进行判断"() {
        when:
        def a = 20+1

        then:
        a > 20
    }
  • thrown
    def "thrown()抛出异常"() {
        when:
        throw new NullPointerException()

        then:
        def e = thrown(Exception)
        e instanceof NullPointerException
    }
  • where
    def "where"() {
        expect:
        Math.max(a, b) == c

        where:
        // 入参||结果
        a  | b  || c //多场景测试,类似于junit Parameterized用法
        1  | 3  || 3
        23 | 11 || 23
    }
    
    def "<< where"() {
        expect:
        Math.max(a, b) == expected

        where:

        a << [66, 1, 11] // 多场景测试用例
        b << [11, 2, 11]

        expected << [66, 2, 11]
    }
    
    def "expect where"() {
        expect:
        Math.min(a, b) == c

        where:
        a | _  //_ 占位符
        1 | _
        2 | _

        b << [2, 1]
        c = a < b ? a : b
    }
  • Mock
    def "Mock"() {
        given:
        def personA = [getName: { "Tom" }] as Person // 类型转换

        Person personB = Mock(Person.class) // 制造假数据
        personB.getName() >> "Jam" // mock方法的返回值

        println personA.getName()
        println personB.getName()
    }
    def "<< 可以向集合中添加元素"() {
        given:
        List<Integer> list = []
        list << 2

        println list[-1] //获取倒数第一个元素

    }
    def ">>"() {
        given:
        def teacher = Mock(Teacher.class)
        def student = new Student(id: 1, name: "jams", age: 21, teacher: teacher)
        student.getStudents(_ as String) >> ["1000"]//若不需要关注如参,可用占位符
        teacher.getName() >> "Tom"

        when:
        def name = student.getTeacherName()
        then:
        name == expectName
        where:
        rst      | expectName
        ["1000"] | "Tom"
    }

junit单元测试可参考http://whvixd.com/2019/10/13/junit4/

groovy基本语法可参考http://whvixd.com/2019/04/08/Groovy/