ClassCastException exception when running Robolectric test with Power Mock on multiple files

拜拜、爱过 提交于 2019-11-30 12:46:46
csu007

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.

  1. Under your unit test directory, src/test/java, create a package directory that is exactly the same as Mockito configuration package, org/mockito/configuration.

  2. So under the full test directory src/test/java/org/mockito/configuration, add a new class named MockitoConfiguration.

  3. Overwrite the enableClassCache() method as following.

    package org.mockito.configuration;
    
        public class MockitoConfiguration extends DefaultMockitoConfiguration {
    
        @Override
        public boolean enableClassCache() {
            return false;
        }
    }
    
  4. 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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!