Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ClassCastException运行2个Roboelectic测试类时出现异常,该测试类具有模拟同一静态类的Power Mock_Java_Unit Testing_Robolectric_Powermock_Android Testing - Fatal编程技术网

Java ClassCastException运行2个Roboelectic测试类时出现异常,该测试类具有模拟同一静态类的Power Mock

Java ClassCastException运行2个Roboelectic测试类时出现异常,该测试类具有模拟同一静态类的Power Mock,java,unit-testing,robolectric,powermock,android-testing,Java,Unit Testing,Robolectric,Powermock,Android Testing,我有两个测试类:MyTest1和MyTest2。它们都有相同的头并测试相同的类,但它们测试该类的两个非常不同的方面(您可能会因此批评我,但让我们不要在这里讨论这一点): 问题是我可以单独运行MyTest1,所有测试都正常运行。我可以单独运行MyTest2,所有测试都正常。但是当我运行MyTest1和MyTest2的所有测试时(它们被编译成一个jar)——当我这样做时,只有第一个运行类(MyTest1)正常,第二个类失败。 除此之外,所有测试都会崩溃: org.mockito.except

我有两个测试类:MyTest1和MyTest2。它们都有相同的头并测试相同的类,但它们测试该类的两个非常不同的方面(您可能会因此批评我,但让我们不要在这里讨论这一点):

问题是我可以单独运行MyTest1,所有测试都正常运行。我可以单独运行MyTest2,所有测试都正常。但是当我运行MyTest1和MyTest2的所有测试时(它们被编译成一个jar)——当我这样做时,只有第一个运行类(MyTest1)正常,第二个类失败。 除此之外,所有测试都会崩溃:

    org.mockito.exceptions.base.MockitoException: 
ClassCastException occurred while creating the mockito proxy :
  class to imposterize : 'com.my.project.MyStrangeClassWithStaticMethods', loaded by classloader : 'org.powermock.core.classloader.MockClassLoader@2617b42c'
  imposterizing class : 'com.my.project.MyStrangeClassWithStaticMethods$$EnhancerByMockitoWithCGLIB$$41f09512', loaded by classloader : 'org.mockito.internal.creation.jmock.SearchingClassLoader@230eec25'
  proxy instance class : 'com.my.project.MyStrangeClassWithStaticMethods$$EnhancerByMockitoWithCGLIB$$41f09512', loaded by classloader : 'org.mockito.internal.creation.jmock.SearchingClassLoader@23a0547b'

You might experience classloading issues, disabling the Objenesis cache *might* help (see MockitoConfiguration)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:109)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:57)
    at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)
    at com.my.project.MyTest2.testSomething(MyTest2.java:66666<no matter what line number>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:52)
    at sun.reflect.GeneratedMethodAccessor26.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:638)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
    at org.robolectric.RobolectricTestRunner$2.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$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.chromium.testing.local.GtestComputer$GtestSuiteRunner.run(GtestComputer.java:46)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
    at org.chromium.testing.local.JunitTestMain.main(JunitTestMain.java:105)
Caused by: java.lang.ClassCastException: Cannot cast com.my.project.MyStrangeClassWithStaticMethods$$EnhancerByMockitoWithCGLIB$$41f09512 to com.my.project.MyStrangeClassWithStaticMethods
    at java.lang.Class.cast(Class.java:3176)
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)
    ... 47 more
org.mockito.exceptions.base.MockitoException:
创建mockito代理时发生ClassCastException:
要冒用的类:“com.my.project.MyStrangeClassWithStaticMethods”,由类加载器加载:“org.powermock.core.classloader”。MockClassLoader@2617b42c'
冒充类:“com.my.project.MyStrangeClassWithStaticMethods$$EnhancerByMockitoWithCGLIB$$41f09512”,由类加载器加载:“org.mockito.internal.creation.jmock”。SearchingClassLoader@230eec25'
代理实例类:“com.my.project.MyStrangeClassWithStaticMethods$$EnhancerByMockitoWithCGLIB$$41f09512”,由类加载器加载:“org.mockito.internal.creation.jmock”。SearchingClassLoader@23a0547b'
您可能会遇到类加载问题,禁用Objensis缓存*可能*帮助(请参阅MockitoConfiguration)
位于org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:109)
位于org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:57)
位于org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)
在com.my.project.MyTest2.testSomething上(MyTest2.java:66666)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:606)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:45)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:42)
位于org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
位于org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
位于org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:52)
位于sun.reflect.GeneratedMethodAccessor26.invoke(未知源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:606)
位于org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
位于org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
位于org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:638)
位于org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
在org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
位于org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
位于org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
位于org.roblectric.roblectrictTestRunner$2.evaluate(roblectrictTestRunner.java:251)
位于org.roblectric.roblectrictTestRunner.runChild(roblectrictTestRunner.java:188)
位于org.roblectric.roblectrictTestRunner.runChild(roblectrictTestRunner.java:54)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
位于org.roblectric.roblectrictTestRunner$1.evaluate(roblectrictTestRunner.java:152)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:300)
位于org.chromium.testing.local.GtestComputer$GtestSuiteRunner.run(GtestComputer.java:46)
位于org.junit.runners.Suite.runChild(Suite.java:128)
位于org.junit.runners.Suite.runChild(Suite.java:24)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:300)
位于org.junit.runner.JUnitCore.run(JUnitCore.java:157)
位于org.junit.runner.JUnitCore.run(JUnitCore.java:136)
位于org.chromium.testing.local.junitestmain.main(junitestmain.java:105)
原因:java.lang.ClassCastException:无法将com.my.project.MyStrangeClassWithStaticMethods$$EnhancerByMockitoWithCGLIB$$41f09512强制转换为com.my.project.MyStrangeClassWithStaticMethods
位于java.lang.Class.cast(Class.java:3176)
位于org.mockito.internal.creation.jmock.ClassImposterizer.importerise(ClassImposterizer.java:62)
... 47多
这次崩溃迫使我将2个类合并为1个测试类。这可能会妨碍我在另一个测试类中使用StaticMethods.class模拟MyStrangeClass。我只能嘲笑一次:(

这个问题看起来有点不同,需要另一个解决方案

Powermock 1.6.0

Mockito 1.10.5


Robolectric 3.0

我找到了一个解决方案。堆栈跟踪显示:

禁用Objensis缓存可能会有所帮助(请参阅MockitoConfiguration)

我做到了,而且成功了。只需将此类添加到您的测试源位置:
    org.mockito.exceptions.base.MockitoException: 
ClassCastException occurred while creating the mockito proxy :
  class to imposterize : 'com.my.project.MyStrangeClassWithStaticMethods', loaded by classloader : 'org.powermock.core.classloader.MockClassLoader@2617b42c'
  imposterizing class : 'com.my.project.MyStrangeClassWithStaticMethods$$EnhancerByMockitoWithCGLIB$$41f09512', loaded by classloader : 'org.mockito.internal.creation.jmock.SearchingClassLoader@230eec25'
  proxy instance class : 'com.my.project.MyStrangeClassWithStaticMethods$$EnhancerByMockitoWithCGLIB$$41f09512', loaded by classloader : 'org.mockito.internal.creation.jmock.SearchingClassLoader@23a0547b'

You might experience classloading issues, disabling the Objenesis cache *might* help (see MockitoConfiguration)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:109)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:57)
    at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)
    at com.my.project.MyTest2.testSomething(MyTest2.java:66666<no matter what line number>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:52)
    at sun.reflect.GeneratedMethodAccessor26.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:638)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
    at org.robolectric.RobolectricTestRunner$2.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$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.chromium.testing.local.GtestComputer$GtestSuiteRunner.run(GtestComputer.java:46)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
    at org.chromium.testing.local.JunitTestMain.main(JunitTestMain.java:105)
Caused by: java.lang.ClassCastException: Cannot cast com.my.project.MyStrangeClassWithStaticMethods$$EnhancerByMockitoWithCGLIB$$41f09512 to com.my.project.MyStrangeClassWithStaticMethods
    at java.lang.Class.cast(Class.java:3176)
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)
    ... 47 more
package org.mockito.configuration;

public class MockitoConfiguration extends DefaultMockitoConfiguration {
    @Override
    public boolean enableClassCache() {
        return false;
    }
}