/**
* Testing Shared preferences with mocking the static methods in Prefs.
* This helps us where a test suite is failing, in long run
*/
@Test
fun testSharedPrefs_returnsMockedPrefs(){
//Mocking all the static methods in Prefs.java
PowerMockito.mockStatic(Prefs::class.java)
//Seting up expectation with Mockito
Mockito.`when`(Prefs.getPetMToken()).thenReturn("TOKEN")
Mockito.`when`(Prefs.appLaunchedBefore()).thenReturn(false)
//with version 2.0.0, verify static is giving errors and working without this verification
PowerMockito.verifyStatic(Prefs::class.java) ---------------> **NotAMockException**
Assert.assertEquals("TOKEN", Prefs.getPetMToken())
PowerMockito.verifyStatic(Prefs::class.java, Mockito.times(1)) ---------------> **NotAMockException**
Assert.assertEquals(false, Prefs.appLaunchedBefore())
}
由于我是新来使用 Mockito/PowerMock 等编写单元测试,因此我正在尝试遵循文档。这里提到我们应该在每次验证行为之前调用 verifyStatic https://github.com/powermock/powermock/wiki/Mockito#how-to-verify-behavior
但是在这里,当我这样做时,它给了我不是模拟异常。
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type Class and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
at test.GroomingAdapterTest.testSharedPrefs_returnsMockedPrefs(GroomingAdapterTest.kt:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
这是我的 build.gradle 配置:
testImplementation 'org.powermock:powermock-api-mockito2:2.0.0-beta.5'
testImplementation 'org.powermock:powermock-core:2.0.0-beta.5'
testImplementation 'org.powermock:powermock-module-junit4:2.0.0-beta.5'
testImplementation 'org.mockito:mockito-core:2.23.0'
// testImplementation 'org.powermock:powermock-api-mockito2:1.7.0RC2'
// testImplementation 'org.powermock:powermock-core:1.7.0RC2'
// testImplementation 'org.powermock:powermock-module-junit4:1.7.0RC2'
// testImplementation 'org.mockito:mockito-core:2.4.0'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
androidTestImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0", {
exclude group: 'org.mockito', module: 'mockito-core'
})
androidTestImplementation 'org.mockito:mockito-android:2.23.0'
注意:- 如果我评论 veirifyStatic() 的行,则测试运行并通过。
Powermock 中是否有不需要我们调用 verifyStatic() 的更改?