Classcastexception Exception When Running Robolectric Test With Power Mock On Multiple Files
Solution 1:
I would suggest to disable ClassCache of Mockito as suggested in the exception message . Here is how I disable Mockito ClassCache by adding a MockitoConfiguration class in Android Studio.
Under your unit test directory, src/test/java, create a package directory that is exactly the same as Mockito configuration package, org/mockito/configuration.
So under the full test directory src/test/java/org/mockito/configuration, add a new class named MockitoConfiguration.
Overwrite the enableClassCache() method as following.
package org.mockito.configuration; publicclassMockitoConfigurationextendsDefaultMockitoConfiguration { @OverridepublicbooleanenableClassCache() { returnfalse; } }
When you run your Unit test under src/java/test, your MockitoConfiguration should be loaded and Mockito class cache should be disabled.
Hope it helps.
Solution 2:
So I solved this problem by adding
@PowerMockIgnore({ "*.*" })
@PrepareForTest({ StaticClass1.class,StaticClass2.class })
That will make Power mock ignore all the classes. For my case PowerMock was classloading the Bus which in my code was actually mocked by Mockito.Once I added the annotations above all the classes in the testsuite worked without any errors.
Post a Comment for "Classcastexception Exception When Running Robolectric Test With Power Mock On Multiple Files"