/**
* 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())
}
As I am new to write unit tests wit Mockito/PowerMock etc I am trying to follow the documentation. Here it is mentioned that we are supposed to call verifyStatic everytime before verifying behavior https://github.com/powermock/powermock/wiki/Mockito#how-to-verify-behavior
But here when I do so, it gives me Not a mock exception.
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)
This is my build.gradle config:
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'
Note:- If I comment the lines for veirifyStatic() then the test runs and passes.
Is there a change in Powermock that doesn't require us to call verifyStatic()?