Mockito 目前已經發展到第二版,但是要在這個版本的 Mockito 上使用 PowerMock,依據官方的說明仍然還停留在試驗性質的版本,目前最新可取得的版本是 1.7.0RC4。
如果使用前一篇文章提到的,以 @Rule 的方式來啟動 PowerMock:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 改成以下的內容:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 可以更精簡:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,所以只要能運作,有點訊息或是使用的是非正式的版本,都還在可接受的範圍內。
0 意見:
張貼留言