spock在springboot中的使用 转

在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

/**
 *
 * @author yawn
 *     2019/6/10 16:14
 */
@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
    }
}

在要运行的测试类上加注解@SpringBootTest,即可在运行这个测试类之前启动spring容器,运行完这个类所有的测试用例之后,容器会自动运行结束。

这三篇文章所有的完整测试代码都在我的gitee: https://gitee.com/yawensilence/demo-spock

本文转载自:jvm123-java技术分享站 

原文链接:http://jvm123.com/2019/08/spock-springboot/

发布了67 篇原创文章 · 获赞 1 · 访问量 1816

猜你喜欢

转载自blog.csdn.net/qq_15060345/article/details/100526363