gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:37:35 25 4
gpt4 key购买 nike

我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com