Android知识Android开发程序员

解决 Spock 与 PowerMock 的集成问题

2017-06-02  本文已影响499人  _WZ_

在前一篇文章中,提到了如何在 Spock 中测试 Static 的 Method,以弥补 Spock 在这个部份的不足。当时使用的是 PowerMock 1.6.2,只不过随着时间的推移,最新的 Mockito 与 PowerMock 组合,在与 Spock 的集成上并不顺利。

Mockito 目前已经发展到第二版,但是要在这个版本的 Mockito 上使用 PowerMock,依据官方的说明仍然还停留在试验性质的版本,目前最新可取得的版本是 1.7.0RC4。

如果使用前一篇文章提到的,以 @Rule 的方式来启动 PowerMock:

import org.junit.Rule
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.rule.PowerMockRule
import spock.lang.Specification

@PrepareForTest([TestClass.class])
class MockStaticMethodSpec extends Specification {

    @Rule
    PowerMockRule mPowerMockRule = new PowerMockRule();

    def "测试静态方法"() {
        setup :
        PowerMockito.mockStatic(TestClass.class)

        when :
        Mockito.when(TestClass.staticMethod()).thenReturn("测试用字串")

        then :
        TestClass.staticMethod() == "测试用字串"
    }

}

原本可以顺利运行的测试用例,在运行测试时会出现 NullPointerException

这个问题在网络上的信息不多,如果无法解决,就要回到 JUnit + Mockito + PowerMock 的方案,原本 Spock 所带来的优势就失去了,是一个很两难的抉择。

所幸在研读 PowerMock 的官方文档时,提到了一个新的功能。在 PowerMock 1.6.0 开始,PowerMockRunner 可以 Delegate 另一个 JUnit Runner,以取代无法使用 JUnit Rule 的情况。

而在 Specification 的 Source Code 中可以看到,Spock 是使用名为 Sputnik 的 JUnit Runner。

所以把之前的 Code 改成以下的内容:

import org.junit.runner.RunWith
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.powermock.modules.junit4.PowerMockRunnerDelegate
import org.spockframework.runtime.Sputnik
import spock.lang.Specification

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Sputnik.class)
@PrepareForTest([TestClass.class])
class MockStaticMethodSpec extends Specification {

    def "测试静态方法"() {
        setup :
        PowerMockito.mockStatic(TestClass.class)

        when :
        Mockito.when(TestClass.staticMethod()).thenReturn("测试用字串")

        then :
        TestClass.staticMethod() == "测试用字串"
    }

}

build.gradle 可以更精简:

apply plugin: 'java'
apply plugin: 'groovy'

dependencies {
    testCompile 'org.codehaus.groovy:groovy-all:2.4.7'
    testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
    testCompile 'org.objenesis:objenesis:2.5.1'
    testCompile 'cglib:cglib-nodep:3.2.5'
    testCompile 'org.mockito:mockito-core:2.8.9'
    testCompile 'org.powermock:powermock-api-mockito2:1.7.0RC4'
    testCompile 'org.powermock:powermock-module-junit4:1.7.0RC4'
}

透过 PowerMockRunner 的 Delegation,在 Spock 1.1 及 1.0 上,基本的功能都可以正常的运行,只不过会有以下额外的讯息出现。

Notifications are not supported for behaviour ALL_TESTINSTANCES_ARE_CREATED_FIRST
Notifications are not supported when all test-instances are created first!

由于不是 Production 的 Code,所以只要能运行,有点讯息或是使用的是非正式的版本,都还在可接受的范围内。

更多深入的文章请参阅 http://www.jianshu.com/u/fea63707e07f

上一篇下一篇

猜你喜欢

热点阅读