千家信息网

springboot中spock如何使用

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章给大家介绍springboot中spock如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。前面介绍了spock测试框架的详细使用,以及如何在spock中使用测试桩。
千家信息网最后更新 2025年02月03日springboot中spock如何使用

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

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

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

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

package com.yawn.spockimport com.yawn.spock.service.CalculateServiceimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.boot.test.context.SpringBootTestimport spock.lang.Specification@SpringBootTestclass 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如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

0