springboot中spock如何使用

这篇文章给大家介绍springboot中spock如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

前面介绍了spock测试框架的详细使用,以及如何在spock中使用测试桩。本文介绍在springboot环境中使用spock。

在spring环境中使用spock,也就是要自动注入被测试的实例,不需要我们手动初始化实例。

这样也就是先启动spring容器,再运行我们的测试用例,在springboot中,很容易做到这一点,甚至比junit还简单。如下:

package com.yawn.spock

import com.yawn.spock.service.CalculateService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import spock.lang.Specification


@SpringBootTest
class SpringBootSpec extends Specification {

    @Autowired
    CalculateService calculateService;

    def "spring boot test"() {
        expect: "asas"
        z == calculateService.minus(x, y)

        where:
        x << [9, 8, 7]
        y << [6, 5, 4]
        z << [3, 3, 3]
    }

    def "spring boot test2"() {
        expect: "asas"
        z == calculateService.minus(x, y)

        where:
        x | y | z
        9 | 8 | 1
        6 | 5 | 1
        3 | 3 | 0
    }
}

关于springboot中spock如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。