使用mockito模拟SessionContex会导致ClassNotFoundException

时间:2015-09-29 10:17:28

标签: java unit-testing junit mockito

我从单元测试开始。我已经对一个没有注入SessionContext的类进行了更改,因此我可以在需要时进行查找。

@Resource
private SessionContext ctx;

现在,在我的测试中,我想注入它,以便我可以模拟查找方法:

@Mock
private SessionContext ctx;

但是当我进行测试时,我得到了:

  

java.lang.NoClassDefFoundError:javax / xml / rpc / handler / MessageContext     at java.lang.Class.getDeclaredMethods0(Native Method)at   java.lang.Class.privateGetDeclaredMethods(Class.java:2615)at at   java.lang.Class.getDeclaredMethods(Class.java:1860)at   org.mockito.cglib.core.ReflectUtils.addAllMethods(ReflectUtils.java:349)     在org.mockito.cglib.proxy.Enhancer.getMethods(Enhancer.java:427)at   org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:457)at at   org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)     在   org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)     在org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)     在org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)     在   org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:110)     在   org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)     在   org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)     在   org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60)     在org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143)     在   org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:84)     在   org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:51)     在   org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:90)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:292)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)     在   org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)     在org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ 1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)     在   org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)     在   org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)     在   org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)     在   org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)     在   org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)     在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:java.lang.ClassNotFoundException:   javax.xml.rpc.handler.MessageContext at   java.net.URLClassLoader $ 1.run(URLClassLoader.java:366)at   java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)at   java.security.AccessController.doPrivileged(Native Method)at   java.net.URLClassLoader.findClass(URLClassLoader.java:354)at   java.lang.ClassLoader.loadClass(ClassLoader.java:425)at   sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)at at   java.lang.ClassLoader.loadClass(ClassLoader.java:358)... 39更多

我觉得很奇怪,因为我有所需的所有依赖项(此代码适用于实际应用程序)。

如何使用mockito模拟和注入SessionContext? (我无法改变模拟框架)。

2 个答案:

答案 0 :(得分:0)

我经历了同样的问题并通过仅在测试范围上添加依赖项来解决!

^(([\%\/\\\&\?\,\'\;\:\!\-])\2?(?!\2))+$

答案 1 :(得分:0)

如果您使用的是以下jboss ee 7规格,则可能会遇到此问题。它以前包含在jboss ee 6规范中,但是从ee 7中删除了一些(我尚未发现)

<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-all-7.0</artifactId>

要解决此问题,请添加以下依赖项。

    <dependency>
        <groupId>org.jboss.spec.javax.xml.rpc</groupId>
        <artifactId>jboss-jaxrpc-api_1.1_spec</artifactId>
        <version>1.0.1.Final</version>
        <scope>provided</scope>
    </dependency>