宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

这篇文章给大家介绍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
    }
}