6

我正在尝试使用 PowerMock 在我的测试中模拟 java ServicesLoader(即final),它似乎失败了......

重现错误的最简单的测试用例是这样的:

import java.util.ServiceLoader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ServiceLoader.class)
public class ServiceLoaderTest {
    @Test
    public void testServiceLoaderMock() {
        ServiceLoader mock = PowerMock.createMock(ServiceLoader.class);
    }
}

我得到的例外是:

-------------------------------------------------------------------------------
Test set: ServiceLoaderTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.407 sec <<< FAILURE!
testServiceLoaderMock(ServiceLoaderTest)  Time elapsed: 0.359 sec  <<< ERROR!
java.lang.IllegalAccessError: java.lang.Class
        at sun.reflect.GeneratedSerializationConstructorAccessor8.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:56)
        at org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.java:257)
        at org.powermock.reflect.Whitebox.newInstance(Whitebox.java:139)
        at org.powermock.core.DefaultFieldValueGenerator.instantiateFieldType(DefaultFieldValueGenerator.java:74)
        at org.powermock.core.DefaultFieldValueGenerator.fillWithDefaultValues(DefaultFieldValueGenerator.java:51)
        at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2146)
        at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:98)
        at ServiceLoaderTest.testServiceLoaderMock(ServiceLoaderTest.java:12)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:322)
        at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
        at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:309)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:297)
        at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
        at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:222)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:161)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
        at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
        at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:133)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:112)
        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)

我想这与Class<T>对服务字段进行一些初始化有关,但我不确定。

解决此问题后,我还想模拟静态方法ServiceLoader.load(Class)以返回先前创建的模拟,因此可能会出现更多问题...

关于如何解决它的任何想法?

4

2 回答 2

0

看看这篇文章:Mocking static methods in java system classes,或者更具体地说,“但是等等,这对最终的系统类有用吗?” .

引自文章:

但是最终的系统类呢?PowerMock 无法删除系统类的 final 修饰符,那该怎么办?PowerMock 在这些情况下所做的是在运行时创建一个全新的类,其结构与原始最终系统类完全相同。即,所有方法名称和它们对应的签名都被复制到这个新的副本类中。为了允许部分模拟副本类的所有静态方法,委托给最终系统类中的原始方法。它也是被底层模拟框架而不是原始系统类模拟的副本类。然后,MockGateway 会发现绑定到这个特定系统类的所有方法都应该路由到副本模拟。因此模拟最终系统类中的静态方法,例如 java.lang.System 或 java.lang。字符串也可以。作为旁注,实际上也可以使用这种技术在 Java 中实现鸭子类型。无论如何,这里有一个例子来展示我们刚才所说的:

 public class SystemPropertyMockDemo {

    public String getSystemProperty() throws IOException {
        return System.getProperty("property");
    }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest( { SystemPropertyMockDemo.class, SystemPropertyMockDemoTest.class })
public class SystemPropertyMockDemoTest

    @Test
    public void demoOfFinalSystemClassMocking() throws Exception {
        mockStatic(System.class);

        expect(System.getProperty("property")).andReturn("my property");

        replayAll();

        assertEquals("my property",
                                  new SystemPropertyMockDemo().getSystemProperty());

        verifyAll();
    }
}
于 2011-04-01T11:17:20.130 回答
0

这似乎是一个错误。从邮件列表中得到答案:

http://groups.google.com/group/powermock/browse_thread/thread/4afafbfdf1b3ce58

于 2011-04-05T13:58:39.133 回答