2016-04-08 95 views
2

當我嘗試在我的單元測試中窺探一個對象時,我收到了一個異常。 這是我的單元測試文件:Mockito在春天的間諜對象

@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()); 
    } 
} 

我在鏈路異常分配bookInfoParams間諜:

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$1.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$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.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$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 java.lang.ClassLoader.loadClass(ClassLoader.java:358) 
    ... 44 more 

這是我的POM文件的依賴

<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> 

也許版本不兼容或我錯過了什麼?

回答

6

這可能是依賴關係的問題。 powermock-api-mockitomockito-core版本1.10.19具有編譯時間依賴性,但是您還定義了對版本2.0.44-beta的依賴關係。這可能是一個類路徑問題。嘗試刪除版本2.0.44-beta上的顯式依賴項。

+0

這絕對有用!謝謝 –

+0

我正在尋找一個小時的解決方案。我嘗試添加cglib和objenesis,但我仍然遇到同樣的問題。這工作。謝謝! – ventura8

3

在發佈2.0.32-beta Mockito團隊將cglib類移入separated jar。但是PowerMock仍然依賴於已經移動的類,所以你得到了java.lang.NoClassDefFoundError。

在版本1.6.2中,Mockito類被複制到PowerMock中,但不是全部。作爲解決方法,我可以建議克隆,構建並添加到類路徑mockito-cglib(https://github.com/mockito/mockito-cglib)。

PowerMock將有supported Mockito 2 API自1.6.5將於5月底發佈,但仍將使用cglib。完整的Mockito 2與Bytebuddy將在1.6.5版本後發佈。

Developer Guide for Migration to Mockito 2

+0

只需刪除mockito-core依賴項,它就可以工作! –

+0

@LeoLe它的解決方案,當你不需要Mockito 2。 –

0

這是因爲powermock-API的的Mockito上的Mockito核依賴的發生。由於powermock-api-mockito被明確添加爲依賴項,請刪除mockito-core,這可能會導致問題