java - 使用模拟相同静态类的 Power Mock 运行 2 个 Roboelectic 测试类时出现 ClassCastException 异常

标签 java unit-testing robolectric powermock android-testing

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

@RunWith(LocalRobolectricTestRunner.class)
@Config(manifest = RobolectricTestData.AndroidManifestFileName)
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest(MyStrangeClassWithStaticMethods.class)
@ParametersAreNonnullByDefault
public class MyTest1 extends MyBaseTest {

    // This is to dinamically load PowerMock. We can't use PowerMockRunner because we already use
    // RobolectricTestRunner and PowerMockRunner doesn't do robolectric's stuff.
    @Rule
    public final PowerMockRule rule = new PowerMockRule();

    /**
     * Tests set up.
     */
    @Before
    public final void setUp() throws Exception {
        super.setUp();
    }
    public void testSomething() {
        PowerMockito.mockStatic(MyStrangeClassWithStaticMethods.class);
        // do some stuff
    }
}

问题是我可以单独运行 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

这次崩溃迫使我将我的 2 个类合并到 1 个测试类中。这可能会阻止我在另一个测试类中模拟 MyStrangeClassWithStaticMethods.class 的可能性。我只能 mock 一次 :(

问题看起来像 ClassCastException exception when running Robolectric test with Power Mock on multiple files但有点不同,需要另一种解决方案。

Powermock 1.6.0

模拟 1.10.5

机器人电子 3.0

最佳答案

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

disabling the Objenesis cache might help (see MockitoConfiguration)

我做到了并且成功了。只需将此类添加到您的测试源位置:

package org.mockito.configuration;

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

关于java - 使用模拟相同静态类的 Power Mock 运行 2 个 Roboelectic 测试类时出现 ClassCastException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828339/

相关文章:

android - Gradle Build Test "Could not normalize...",用于在 Android Studio 中进行 Robolectric 测试

java - AbstractTableModel 中的空指针

java - 在gradle中将本地jar文件添加到项目的依赖项

java - io.github.dropwizard-jobs 的 Maven 依赖

java - 通过名称 MyClass.class 加载类 - 这可能吗?

C# 反射 : Replace a referenced assembly

android - 使用 Robolectric 模拟单击​​菜单项时出错

ios - 为什么不在 Xcode 中编译测试?

带有 Robolectric : my class test is not found 的 Android Studio

android - 使用 robolectric 和 eclipse 运行 android 测试用例时出现 TypeNotPresentException