Using Spock to test Java or Groovy applications

Before

Spock是用于groovy项目的单元测试框架,这个框架简单易用,值得推广。

Coding

<dependencies>
	<dependency>
    	<groupId>org.codehaus.groovy</groupId>
    	<artifactId>groovy-all</artifactId>
    	<version>2.4.4</version>
	</dependency>
	<dependency>
    	<groupId>junit</groupId>
    	<artifactId>junit</artifactId>
   	 	<version>4.4</version>
    	<scope>test</scope>
	</dependency>
	<dependency>
   		<groupId>org.spockframework</groupId>
    	<artifactId>spock-core</artifactId>
    	<version>1.0-groovy-2.4</version>
    	<scope>test</scope>
	</dependency>
	<dependency>
    	<groupId>org.spockframework</groupId>
    	<artifactId>spock-spring</artifactId>
    	<version>1.0-groovy-2.4</version>
    	<scope>test</scope>
	</dependency>
</dependencies>
 
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <version>1.5</version>
        </plugin>
    </plugins>
</build>

 

def "test when then expect"() {
        when:
        def x = Math.max(1, 2)

        then:
        x == 2

        //可以简化成下面的
        expect:
        Math.max(1, 2) == 2
    }

    def "test setup or given"() {
//        setup:
//        def stack = new Stack()
//        def elem = "push me"

        //setup 与 given 等价

        given:
        def stack = new Stack()
        def elem = "push me"

        when:
        stack.push(elem)

        then:
        !stack.empty
        stack.size() == 1
        stack.peek() == elem

        when:
        stack.pop()

        then:
        stack.empty

        when:
        stack.pop()

        then:
        //notThrown(EmptyStackException)
        def e = thrown(EmptyStackException)
        e.cause == null
    }

    def "test cleanup"() {
        setup:
        def stack = new Stack()
        def elem = "push me"

        when:
        stack.push(elem)

        then:
        elem == stack.pop()

        cleanup:
        stack = null
        elem = null
    }


    def "HashMap accepts null key"() {
        setup:
        def map = new HashMap()

        when:
        map.put(null, "elem")

        then:
        //thrown(NullPointerException)
        notThrown(NullPointerException)
    }


    def "maximum of two numbers"() {
        expect:
        // exercise math method for a few different inputs
        Math.max(1, 3) == 3
        Math.max(7, 4) == 7
        Math.max(0, 0) == 0
    }

    def "maximum of two numbers2"() {
        expect:
        Math.max(a, b) == c

        where:
        a | b || c
        3 | 5 || 5
        7 | 0 || 7
        0 | 0 || 0
    }

    @Unroll
    def "maximum of #a and #b should be #c"() {
        expect:
        Math.max(a, b) == c

        //Unroll 当成三个方法执行,否则就是在for循环里面执行
        where:
        a | b || c
        3 | 5 || 5
        7 | 0 || 7
        0 | 0 || 0
    }

explain

如上方代码所见,spock每个feature method被划分为不同的block,不同的block处于测试执行的不同阶段,在测试运行时,各个block按照不同的顺序和规则被执行,如下:

package org.spockframework.runtime.model;
public enum BlockKind {
    SETUP,//初始化资源,最前执行(与given等价)
    EXPECT,//期望,等同于assert(assert支持需要运行时配置VMOptions:java -ea)
    WHEN,//与then一起等同于expect
    THEN,//与when一起等同于expect
    CLEANUP,//清理资源,最后执行
    WHERE;//循环跑测试
}

 

spock框架重点就是上面这些block,搞定!!!

转载于:https://my.oschina.net/chenxiaojie/blog/835937

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值