使用Mockito在JUnit中使用PowerMock和PowerRule获取javassist.NotFoundException

时间:2012-08-30 05:25:22

标签: java maven junit4 mockito powermock

我已将JUnit中的PowerMock和PowerRule与Mockito集成。

以下是我的依赖项:

<dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.0.GA</version>
      </dependency>
<dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.3.1</version>
</dependency>

 <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
 </dependency>
 <dependency>
        <groupId>org.powermoc</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.4.12</version>
    <scope>test</scope>
    </dependency>
 <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.4.12</version>
    <scope>test</scope>
 </dependency>
 <dependency>
       <groupId>org.powermock</groupId>
       <artifactId>powermock-module-junit4-rule</artifactId>
       <version>1.4.12</version>
       <scope>test</scope>
 </dependency>
 <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-classloading-objenesis</artifactId>
        <version>1.4.12</version>
        <scope>test</scope>
 </dependency>

我的测试课程是:

public class TestClass extends AbstractShiroTest{
    @Rule
    public PowerMockRule rule = new PowerMockRule();
    @Autowired
    SomeService someService;
    @Before

    public void setUp(){
    Map<String, Object> newMap = new HashMap<String, Object>();
    newMap.put("userTimeZone", "Asia/Calcutta");
    Subject subjectUnderTest = mock(Subject.class);
             when(subjectUnderTest.getPrincipal()).thenReturn(LMPTestConstants.USER_NAME);
    Session session = mock(Session.class);
    when(session.getAttribute(LMPCoreConstants.USER_DETAILS_MAP)).thenReturn(newMap);
    when(subjectUnderTest.getSession(false)).thenReturn(session);
    setSubject(subjectUnderTest);
    PowerMockito.mockStatic(CasSessionUtil.class);
    when(CasSessionUtil.getCarrierId()).thenReturn(1L);
}

  @Test

public void myTestMethod()  {
someService.doSomething();
 }
}

doSomething正在调用我需要模拟的静态方法。 当我运行我的测试用例时,我得到javassist.NotFoundException: $Proxy88

完整堆栈跟踪:

  

java.lang.RuntimeException:javassist.NotFoundException:$ Proxy88       在org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:187)       在org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:147)       在org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)       at java.lang.ClassLoader.loadClass(ClassLoader.java:252)       at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)       at java.lang.Class.forName0(Native Method)       at java.lang.Class.forName(Class.java:247)       在org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:66)       在org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:26)       在org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:243)       在org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)       在org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)       在org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)       在org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)       在org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)       在org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)       在org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)       在org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)       在org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)       在org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)       在org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)       在org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)       在org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)       在org.powermock.classloading.DeepCloner.clone(DeepCloner.java:82)       在org.powermock.classloading.DeepCloner.clone(DeepCloner.java:69)       在org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)       在org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)       在org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)       在org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:193)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)       在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)       在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)       在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)       在org.junit.runners.ParentRunner.run(ParentRunner.java:236)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)       在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)   引起:javassist.NotFoundException:$ Proxy88       at javassist.ClassPool.get(ClassPool.java:436)       在org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:180)       ......还有46个

如果我将依赖项更改为

<dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-classloading-xstream</artifactId>
            <version>1.4.12</version>
            <scope>test</scope>
        </dependency>

然后我得到一个不同的例外。请参阅https://stackoverflow.com/questions/12176049/suggest-work-around-for-com-thoughtworks-xstream-converters-conversionexception(已删除SO问题,需要10k)。

我甚至尝试过使用javassist 15版本,但这也存在同样的问题。

4 个答案:

答案 0 :(得分:8)

自己找到解决方案:

仅使用以下依赖项(适用于Power Mock和Power Rule)

    <!-- Required for PowerMock -->
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- Required for PowerMockRule -->
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4-rule-agent</artifactId>
        <version>1.4.12</version>
        <scope>test</scope>
    </dependency>

现在我没有得到上述任何一个例外

答案 1 :(得分:4)

将powermock-module-junit4-rule替换为powermock-module-junit4-rule-agent。

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4-rule-agent</artifactId>
    <scope>test</scope>
</dependency>

基于代理的引导程序和基于类加载的引导程序之间的主要区别在于您不会遇到类加载问题。

答案 2 :(得分:2)

如果没有更多代码示例,我会收集代码在测试中使用Spring。所以我认为这个错误的原因是与Spring一起使用,它似乎生成了JDK代理($Proxy88)。

Powermock的工作方式是在新的类加载器中运行JUnit测试以修改这些类的字节码,不幸的是,只能从真实文件或至少从某个位置修改字节码。在那里可以读取类二进制文件,因为java无法访问已经加载到JVM中的字节码。 (有可能以有限的方式与代理商合作)。

由于磁盘上不存在JDK代理,因此无法读取或复制到特定的Powermock类加载器。

您正在编写的测试不是单元测试,因为它是使用Spring上下文运行的。您可能想先写一个真正的 单元测试 。然后是一些 集成测试 ,其中您不需要模拟

此外,你应该避免使用静力学,因为它是一个可测试的噩梦。您应该以不需要模拟静态调用的方式重写生产代码。

干杯,

答案 3 :(得分:0)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/test-servlet.xml")
public class ControlCenterManagerImplTest {

@Rule
public  PowerMockRule rule = new PowerMockRule();

//Powermock agent initialization not required and using maven dependency specified above by BHUVAN we can execute Power mock using spring.}