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如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


标题名称:springboot中spock如何使用
路径分享:http://scyanting.com/article/jcjips.html