java Mockito 在 Spring 中的间谍对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36505741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spy object by Mockito in Spring
提问by Leo Le
When I try to Spy an object in my unit test, I got an exception. This is my unit test file:
当我尝试在单元测试中监视对象时,出现异常。这是我的单元测试文件:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/applicationContext.xml" })
public class BookingSuperManTest {
BookInfoParams bookInfoParams;
HttpAttributeParams httpAttributeParams;
AbstractRequester requester;
public void beforeStartTest(){
bookInfoParams = Mockito.spy(new BookInfoParams());
httpAttributeParams = Mockito.spy(new HttpAttributeParams());
}
@Test
public void step1GoToHomePage() throws BookingException{
beforeStartTest();
requester = new Step1HomePage(bookInfoParams, httpAttributeParams);
requester.executeRequest();
Assert.assertNotNull(httpAttributeParams.getResponseGetRequest());
}
}
I got the exception at the link assign bookInfoParams spy:
我在链接assign bookInfoParams spy 得到了异常:
java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor
at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.<init>(PowerMockMaker.java:43)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:379)
at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:61)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)
at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:13)
at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:12)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:23)
at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.Mockito.<clinit>(Mockito.java:1103)
at ive.core.test.webbot.book.vietjet.BookingSuperManTest.beforeStartTest(BookingSuperManTest.java:46)
at ive.core.test.webbot.book.vietjet.BookingSuperManTest.step1GoToHomePage(BookingSuperManTest.java:54)
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.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.0.44-beta</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.1</version>
<scope>test</scope>
</dependency>
0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.mockito.cglib.proxy.MethodInterceptor
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 44 more
This is the dependencies in my pom file
这是我的 pom 文件中的依赖项
##代码##Maybe the version is not compatible or I missed something ?
也许版本不兼容或者我错过了什么?
回答by rapasoft
It might be problem with the dependencies. The powermock-api-mockito
has compile time dependency on mockito-core
version 1.10.19
, but you have also defined a dependency on version 2.0.44-beta
. This might be a classpath problem. Try removing the explicit dependency on version 2.0.44-beta
.
依赖关系可能有问题。在powermock-api-mockito
对编译时依赖mockito-core
的版本1.10.19
,但你还定义版本上的依赖2.0.44-beta
。这可能是类路径问题。尝试删除对 version 的显式依赖2.0.44-beta
。
回答by Arthur Zagretdinov
In the release 2.0.32-beta Mockito team moved cglib classes into separated jar. But PowerMock still depends on classes which have been moved, so you get java.lang.NoClassDefFoundError.
在 2.0.32-beta 版本中,Mockito 团队将 cglib 类移到了单独的 jar 中。但是 PowerMock 仍然依赖于已经移动的类,所以你会得到 java.lang.NoClassDefFoundError。
In release 1.6.2 Mockito classes was copied into PowerMock, but not all. As workaround I may suggest clone, build and add to classpath the mockito-cglib (https://github.com/mockito/mockito-cglib).
在 1.6.2 版中,Mockito 类被复制到 PowerMock 中,但不是全部。作为解决方法,我可能会建议克隆、构建并添加到类路径 mockito-cglib ( https://github.com/mockito/mockito-cglib)。
The PowerMock will have supportedMockito 2 API since 1.6.5 which will have been released by the end of the May, but will still use cglib. The full Mockito 2 with Bytebuddy will be supported in release after 1.6.5.
PowerMock自 5 月底发布的 1.6.5 起将支持Mockito 2 API,但仍将使用 cglib。1.6.5 之后的版本将支持带有 Bytebuddy 的完整 Mockito 2。
回答by ambarish kumar
This is happening because of the dependency of powermock-api-mockito on mockito-core . Since powermock-api-mockito is added explicitly as dependency please remove mockito-core, this might be causing the issue
这是因为 powermock-api-mockito 依赖于 mockito-core 。由于 powermock-api-mockito 被明确添加为依赖项,请删除 mockito-core,这可能会导致问题