从下面的stacktrace可以看出,我试图模拟一个otto Bus实例.似乎在第一个测试类上正确地模拟,但是在第二个类中,我得到这个类转换异常.
在堆栈跟踪我建议禁用Objenisis缓存,但我不知道如何完成,如果这是根本原因,因为我正在使用classloading-xstream:1.6.2根据上面的Robolectric Wiki链接.
如果我运行一个单独的JUnit测试类,我的设置是很好的,但一旦我尝试在一个包中运行所有的测试,只有第一个测试可以工作,后续的测试将会得到类转换异常.
org.mockito.exceptions.base.MockitoException: ClassCastException occurred while creating the mockito proxy : class to mock : 'com.squareup.otto.Bus',loaded by classloader : 'org.robolectric.internal.bytecode.InstrumentingClassLoader@1593948d' created class : 'com.squareup.otto.Bus$$EnhancerByMockitoWithcglib$a3b196',loaded by classloader : 'org.robolectric.internal.bytecode.InstrumentingClassLoader@1593948d' proxy instance class : 'com.squareup.otto.Bus$$EnhancerByMockitoWithcglib$a3b196',loaded by classloader : 'org.mockito.internal.creation.util.SearchingClassLoader@618ff5c2' instance creation by : ObjenesisInstantiator You might experIEnce classloading issues,disabling the Objenesis cache *might* help (see MockitoConfiguration) at org.powermock.API.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:61) at org.powermock.API.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49) at org.powermock.API.mockito.repackaged.cglibMockMaker.createMock(cglibMockMaker.java:24) at org.powermock.API.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:45) at com.acme.androID.myapp.services.gcm.handlers.RequestlogoutHandlerTest.setup(RequestlogoutHandlerTest.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:251) at org.robolectric.RobolectricTestRunner.runchild(RobolectricTestRunner.java:188) at org.robolectric.RobolectricTestRunner.runchild(RobolectricTestRunner.java:54) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access解决方法 我建议按照异常消息中的建议禁用Mockito的ClassCachepackage org.mockito.configuration; public class MockitoConfiguration extends DefaultMockitoConfiguration { @OverrIDe public boolean enableClassCache() { return false; }}0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:152) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runchild(Suite.java:128) at org.junit.runners.Suite.runchild(Suite.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IDeaTestRunner.startRunnerWithArgs(JUnit4IDeaTestRunner.java:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) Caused by: java.lang.classCastException: Cannot cast com.squareup.otto.Bus$$EnhancerByMockitoWithcglib$a3b196 to com.squareup.otto.Bus at java.lang.class.cast(Class.java:3369) at org.powermock.API.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:59) at org.powermock.API.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49) at org.powermock.API.mockito.repackaged.cglibMockMaker.createMock(cglibMockMaker.java:24) at org.powermock.API.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:45) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59) at org.mockito.Mockito.mock(Mockito.java:1285) at org.mockito.Mockito.mock(Mockito.java:1163) ... 36 more org.mockito.exceptions.base.MockitoException: ClassCastException occurred while creating the mockito proxy : class to mock : 'com.squareup.otto.Bus',disabling the Objenesis cache *might* help (see MockitoConfiguration) at org.powermock.API.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:61) at org.powermock.API.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49) at org.powermock.API.mockito.repackaged.cglibMockMaker.createMock(cglibMockMaker.java:24) at org.powermock.API.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:45) at com.acme.androID.myapp.services.gcm.handlers.RequestlogoutHandlerTest.setup(RequestlogoutHandlerTest.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:251) at org.robolectric.RobolectricTestRunner.runchild(RobolectricTestRunner.java:188) at org.robolectric.RobolectricTestRunner.runchild(RobolectricTestRunner.java:54) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:152) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runchild(Suite.java:128) at org.junit.runners.Suite.runchild(Suite.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IDeaTestRunner.startRunnerWithArgs(JUnit4IDeaTestRunner.java:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:6
.
这是我如何通过在AndroID Studio中添加一个MockitoConfiguration类来禁用Mockito ClassCache.
>在您的单元测试目录src / test / java下,创建与Mockito配置包,org / mockito / configuration完全相同的包目录.
>所以在完整的测试目录下的src / test / java / org / mockito / configuration下,添加一个名为MockitoConfiguration的新类.
>覆盖enableClassCache()方法如下.
>当您在src / java / test下运行单元测试时,应加载MockitoConfiguration,并禁用Mockito类缓存.
希望它有帮助.
总结以上是内存溢出为你收集整理的单元测试 – 当Power Mock在多个文件上运行Robolectric测试时,ClassCastException异常全部内容,希望文章能够帮你解决单元测试 – 当Power Mock在多个文件上运行Robolectric测试时,ClassCastException异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)