ClassNotFound使用mockito模拟类时出错

时间:2012-01-10 15:44:50

标签: java mockito powermock

我正在尝试使用Mockito和使用PowerMock来模拟一个类。 这是测试

@RunWith(PowerMockRunner.class)
@PrepareForTest(value = Util.class)
public final class FSNS_MLFTUnitTests
{
    @Test
    public final void testChecksum()
    {
        final Util mockedServiceB = mock(Util.class);
        try
        {
            whenNew(Util.class).withNoArguments().thenReturn(mockedServiceB);
        }
       catch (final Exception e)
       {
            System.out.println("Exception thrown: " + e);
       }
}

尝试运行测试时出现以下错误。

java.lang.ExceptionInInitializerError
at sun.reflect.GeneratedSerializationConstructorAccessor6.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:501)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:120)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:60)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:79)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:53)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:80)
at com.cerner.edc.ccm.host.drivers.fsns.mlft.FSNS_MLFTUnitTests.testChecksum(FSNS_MLFTUnitTests.java:23)
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:592)
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.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:44)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.system.exception.ExceptionAdapter
    at com.util.lang.ClassAssistant.lookupClass(ClassAssistant.java:50)
    at com.util.lang.ClassAssistant.lookupClass(ClassAssistant.java:66)
    at com.logging.edc.EdcAssistant.getResourceBundle(EdcAssistant.java:113)
    at com.logging.edc.EdcLogger.<init>(EdcLogger.java:26)
    at com..logging.edc.EdcLoggerManager.getLogger(EdcLoggerManager.java:50)
    at com.framework.Util.<clinit>(Util.java:65)
    ... 36 more

这个错误在mock(Util.class)上抛出。

以下是我正在使用的pom依赖项:

<dependency>
  <groupId>org.powermock.modules</groupId>
  <artifactId>powermock-module-junit4</artifactId>
  <version>1.3.1</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.powermock.api</groupId>
  <artifactId>powermock-api-mockito</artifactId>
  <version>1.3.1</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.6</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.8.0</version>
</dependency>

任何帮助都是适用的。

谢谢!

1 个答案:

答案 0 :(得分:4)

让我们分析一下堆栈跟踪的底部:

Caused by: com.cerner.system.exception.ExceptionAdapter
    at com.cerner.system.util.lang.ClassAssistant.lookupClass(ClassAssistant.java:50)

我认为这里很清楚:com.cerner.system.util.lang.ClassAssistant的第50行导致了com.cerner.system.exception.ExceptionAdapter

    at com.cerner.system.util.lang.ClassAssistant.lookupClass(ClassAssistant.java:66)
    at com.cerner.system.instrument.logging.edc.EdcAssistant.getResourceBundle(EdcAssistant.java:113)
    at com.cerner.system.instrument.logging.edc.EdcLogger.<init>(EdcLogger.java:26)
    at com.cerner.system.instrument.logging.edc.EdcLoggerManager.getLogger(EdcLoggerManager.java:50)

似乎com.cerner.system.instrument.logging.edc.EdcLoggerManager的实际构造是尝试查找某些类以便获取记录器

    at com.cerner.edc.ccm.host.drivers.framework.Util.<clinit>(Util.java:65)

在这里,您可以看到<clinit>而不是方法名称,这意味着它是类的静态部分。这意味着JVM无法正确加载Util类,因为当ExceptionAdapter 获取记录器时会引发EdcLoggerManger(第65行) Util类)。

然后在后续尝试实例化Util类时,JVM会说这个类没有找到,即你的ClassNotFound

如何解决? 我没有关于您的实际代码的更多信息。但是你应该检查为什么这一行com.cerner.system.util.lang.ClassAssistant.lookupClass(ClassAssistant.java:66)实际上是在抛出一个com.cerner.system.exception.ExceptionAdapter

或者你最终可以模拟EdcLoggerManager.getLogger(...)。

作为一个提醒,如果这段代码不是遗留的,我真的鼓励你避免使用PowerMock,因为它不能防止你设计糟糕的代码(代码可测性差,可变性差,可维护性差)。而是在适当的时候采用真正的OOP设计和良好的实践和模式。