为什么要用Spock写单元测试

为什么要写单元测试

XML之父Tim Bray最近在博客里有个好玩的说法:“代码不写测试就像上了厕所不洗手……单元测试是对软件未来的一项必不可少的投资。”具体来说,单元测试有哪些收益呢?

Spock Example

spock写出的单元测试可以很容易理解,对于输入输出及判定很容易看出。

class MathSpec extends Specification {
  def "maximum of two numbers"(int a, int b, int c) {
    expect:
    Math.max(a, b) == c

    where:
    a | b | c
    1 | 3 | 3
    7 | 4 | 7
    0 | 0 | 0
  }
}

究极Maven配置

配置包含了 groovy + spock + powermock

   <properties>
        <!--        spock version start        -->
        <spock.version>1.3-groovy-2.4</spock.version>
        <groovy.version>2.4.21</groovy.version>
        <powermock.version>2.0.4</powermock.version>
        <jmockit.version>1.39</jmockit.version>
        <junitparaams.versopm>1.1.0</junitparaams.versopm>
        <!--        spock version end        -->
    </properties>
 <dependencies>


        <!--        spock test dependency start        -->
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>${groovy.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>${spock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-spring</artifactId>
            <version>${spock.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.28.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>

        <!--        db test        -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.dbunit</groupId>
            <artifactId>dbunit</artifactId>
            <version>2.5.1</version>
            <scope>test</scope>
        </dependency>
        <!--        db end        -->
        <!--        spock test dependency end        -->


    </dependencies>

项目单元测试生成

TestMe插件可以一键生成Spock、Junit等测试代码,简单修改就可以完成高代码覆盖


Example (Use Generator -> TestMe)

高阶单元测试用法

  1. Mock静态方法
    • 注解@PrepareForTest()
  2. 禁止静态代码块初始化,e.g. -> private static final User user=new User();
    • @SuppressStaticInitializationFor(["example.com.AbbreviationProvinceUtil"])

关联文档

Spock单元测试框架介绍以及在美团优选实战

Spock 单元测试官方文档