其他分享
首页 > 其他分享> > springboot集成spock进行单元测试

springboot集成spock进行单元测试

作者:互联网

1. springboot2.X 集成 spock-spring 进行单元测试,在 pom 中添加 spock 依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.spockframework/spock-spring -->
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-spring</artifactId>
            <version>1.3-groovy-2.5</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.spockframework/spock-core -->
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.3-groovy-2.5</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.5.8</version>
            <type>pom</type>
            <scope>test</scope>
        </dependency>

添加两个plugin用于编译 groovy 代码和使用spock测试的类名规则

<!-- Mandatory plugins for using Spock -->
            <plugin>
                <!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
                visit https://github.com/groovy/GMavenPlus/wiki -->
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.11.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compileTests</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Optional plugins for using Spock -->
            <!-- Only required if names of spec classes don't match default Surefire patterns (`*Test` etc.) -->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <configuration>
                    <useFile>false</useFile>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*Spec.java</include>
                    </includes>
                </configuration>
            </plugin>

 

2 在项目中新加如下测试目录结构

 

 

 标记 groovy 目录为 test source root

3 spock 中的代码块和junit对应关系

 

 

 4 常用的模式有

初始化/执行/期望
given:
when:
then:


期望/数据表
expect:
where:

4.1 数据表中每个测试都是相互独立的,都是specification class该类型的一个具体实例,每条都会执行 setup() cleanup()方法,

4.2 常用的指令有

@Shared //共享
@Timeout //超时时间
@Ignore //忽略该方法
@IgnoreRest //忽略其他方法
@FailsWith //有些问题暂时没有解决
@Unroll // 每个循环独立报告

 

 

参考资料:

http://spockframework.org/spock/docs/1.3/all_in_one.html#_using_code_with_code_for_expectations

 

标签:groovy,springboot,spring,单元测试,spock,spockframework,test,org
来源: https://www.cnblogs.com/zhaopengcheng/p/13965778.html