Spock单元测试快速入门

Spock单元测试入门

导入Maven依赖

<!-- Junit单元测试 -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

<!-- Spock依赖 -->
<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>1.3-groovy-2.4</version>
    <scope>test</scope>
</dependency>

<!-- Spock需要的groovy依赖 -->
<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.4.15</version>
</dependency>

<!-- spring boot spock -->
<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>1.2-groovy-2.4</version>
    <scope>test</scope>
</dependency>
        

创建第一个Spock单元测试类

 新建一个Groovy Class,命名为Test。
在这里插入图片描述

/**
 * spock单测类
 */
class Test extends Specification{
    //before setup
    def setupSpec() {
        println ("setupSpec");
    }
    //setup
    def setup() {
        println ("setup");
    }
    //cleanup
    def cleanup() {
        println ("cleanup");
    }
    //after cleanup
    def cleanupSpec() {
        println ("cleanupSpec");
    }
    //测试方法
    def "testMethod"(){
        println "hello world"
        expect:"期望结果"
        1<2
    }
    
}

 点击运行testMethod()方法,即可看到控制台输出:
在这里插入图片描述

在这里插入图片描述

setup、cleanup方法介绍

 通过控制台的输出,可以看到setup()在测试方法执行前运行,可以进行一些测试前的预热操作。cleanup()在测试方法之后运行,可以用作一些扫尾操作。类似于@Before和@After。

 而setupSpec()方法在setup()之前执行,cleanupSpec()在cleanup()方法之后执行。可以理解为对setup()和cleanup()方法的增强。

spock语句块介绍

expect

def "method1"() {
        expect:"期望结果"
        Math.max(1, 3) == 3
        Math.max(7, 4) == 7
        Math.max(0, 0) == 0
    }

 expect用于期望结果的校验,比如对比两个数的最大值,1,3的最大值为3,符合期望结果,测试通过。如果执行结果和期望结果不符合,则报错。

    def "method1"() {
        expect:"期望结果"
        Math.max(1, 3) == 1
        Math.max(7, 4) == 7
        Math.max(0, 0) == 0
    }

执行报错
在这里插入图片描述

given----expect组合

    def "method2"() {
        given:"准备变量"
        def a = 1
        def b = 2
        
        expect:"期望结果"
        a < b
    }

 given语句块通常用于单测前的一些数据准备操作,配合expect一起使用。

given----when----then组合

    def "method3"(){
        given:"准备变量"
        def num1=-10
        def num2=-2

        when:"调用方法"
        def res1= Math.abs(num1)
        def res2= Math.abs(num2)

        then:"期望结果"
        res1==10
        res2==2
    }

 given:单测前的准备
 when:当调用方法取得结果
 then:判断是否符合期望结果

given----when----then----where组合

    def "method4"(){
        given:"准备变量"
        String str="aaabbbcd"

        when:"调用方法"
        def res1=str.contains(str1)
        def res2=str.contains(str2)

        then:"期望结果"
        res1==true
        res2==true

        where:
        str1    |str2
        "aaa"   |"bbb"
        "ab"    |"bc"
    }

 where用于多数据依次测试。when语句块中并未对str1和str2变量赋值,如果我们需要多组数据进行测试的话,可以使用where语句块,用表格的形式依次给变量进行赋值。

@Unroll 标签的使用

    @Unroll
    def "method4"(){
        given:"准备变量"
        String str="aaabbbcd"

        when:"调用方法"
        def res1=str.contains(str1)
        def res2=str.contains(str2)

        then:"期望结果"
        res1==true
        res2==true

        where:
        str1    |str2
        "aaa"   |"bbb"
        "ab"    |"bc"
        "ccdf"  |"g"
    }

 method4()方法新增一组测试用例"ccdf" |“g”,该组用例会导致测试结果与期望不符,如果方法体上加上@Unroll注解,在控制台可以更具体的看到报错信息,会显示具体第几组数据测试失败。
在这里插入图片描述

与SpringBoot结合使用

@SpringBootTest
class Test extends Specification{
    @Autowired
    UserService userService

    @Unroll
    @Transactional
    @Rollback
    def "method5"(){
        given:
        User user=new User()
        user.setId(id)
        user.setName(name)
        userService.insert(user)

        when:
        User res=userService.getById(id)

        then:
        res.getId()==id
        res.getName()==name

        where:
        id<<[1,2,3]
        name<<["张三","李四","赵五"]
        
//		类似于这种写法
//        id  |name
//        1   |"张三"
//        2   |"李四"
//        3   |"赵五"
    }
}

 在测试类顶部加上@SpringBootTest标签,即可将Spock测试类与SpringBoot进行结合,这样我们可以在测试类中进行@Autowired等操作,通过Spring容器获得service类,进行功能测试。还可以配合@Transactional和@Rollback注解,使方法体内的数据库操作通过事务的方式执行,并且测试完后进行回滚,避免造成脏数据。
 个人拙见,不足望指。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Groovy和Spock是一对非常强大的测试工具,经常被用于Java应用程序的单元测试和集成测试。Groovy是一种动态语言,它能够与Java无缝集成,提供了更简洁、灵活的语法和更强大的功能。Spock是一种基于Groovy的测试框架,它结合了传统的单元测试框架和行为驱动开发(BDD)的思想,提供了一种更易读、更易维护的测试编写方式。 使用Groovy和Spock进行测试非常简单。首先,你需要在项目中引入相应的依赖。对于Groovy,你可以在项目的构建工具(如Maven或Gradle)中添加Groovy的依赖。对于Spock,你需要添加spock-core和spock-spring(如果需要与Spring集成)这两个依赖。 在编写测试时,你可以使用Spock提供的各种注解和断言来编写测试逻辑。Spock的语法非常接近自然语言,能够更好地表达测试的意图。以下是一个简单的示例: ```groovy import spock.lang.Specification class MySpec extends Specification { def "test addition"() { given: def a = 2 def b = 3 when: def result = a + b then: result == 5 } } ``` 在这个示例中,我们定义了一个名为"test addition"的测试方法。在given块中,我们初始化了两个变量a和b。在when块中,我们执行了相加操作并将结果赋给result变量。在then块中,我们使用断言来验证结果是否等于5。 你可以使用任何Groovy和Java的特性来编写测试逻辑,包括使用Mockito等库进行模拟和依赖注入。 希望这能帮助你入门Groovy和Spock测试!如果你有更多的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值