2014-01-15 44 views
1

我正在使用Mockito框架爲我的Adobe CQ5項目創建用於編寫junit的模擬對象。我有一些我想用JUnit測試的服務。如何在adobe cq5中使用mockito創建頁面模擬對象,用於編寫junit

我發現了一個奇怪的問題,當我想用​​下面的代碼它提供了以下錯誤以便創建一個模擬Page「對象:

Page pageObj = Mockito.mock(Page.class); 

但是,這不是與其他API類新/接口

的情況下
java.lang.NoClassDefFoundError: [Lcom/day/cq/tagging/Tag; 
    at java.lang.Class.getDeclaredMethods0(Native Method) 
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source) 
    at java.lang.Class.getDeclaredMethods(Unknown Source) 
    at org.mockito.cglib.core.ReflectUtils.addAllMethods(ReflectUtils.java:349) 
    at org.mockito.cglib.proxy.Enhancer.getMethods(Enhancer.java:427) 
    at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:457) 
    at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) 
    at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) 
    at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378) 
    at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318) 
    at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:93) 
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:50) 
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:54) 
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:44) 
    at org.mockito.Mockito.mock(Mockito.java:882) 
    at org.mockito.Mockito.mock(Mockito.java:777) 
    at nz.co.telecom.telecomcms.components.PageHelperTest.testIsPageBelowOtherPage(PageHelperTest.java:42) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    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: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: java.lang.ClassNotFoundException: com.day.cq.tagging.Tag 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 43 more 

如果我使用任何其他類來創建模擬對象,那麼它工作正常,我

`PageManager page = mock(PageManager.class)`;//or i use annotations 

請告訴我爲什麼只有在Adobe CQ5中的Page類的情況下才給出錯誤。

回答

2

嘗試添加cq-tagging依賴項到您的項目。異常涉及到無法找到com.day.cq.tagging.Tag類。最重要的部分是:

java.lang.NoClassDefFoundError: [Lcom/day/cq/tagging/Tag; 

檢查CQ系統控制檯依賴取景器:

http://localhost:4502/system/console/depfinder?plugin.find=com.day.cq.tagging.Tag&plugin.submit=Find

表明,類可以在com.day.cq.cq-tagging包被發現。作爲Maven的依賴(我cq5.5實例)是:

<dependency> 
    <groupId>com.day.cq</groupId> 
    <artifactId>cq-tagging</artifactId> 
    <version>5.5.0</version> 
    <scope>provided</scope> 
</dependency> 

如果您正在使用CQ/AEM的另一個版本,您可以有不同的版本。並稱依賴後,我可以成功運行下面的單元測試:

import static org.mockito.Mockito.mock; 

import org.junit.Test; 
import com.day.cq.wcm.api.Page; 

public class PageUnitTest { 

    @Test 
    public void shouldMockPageObject() { 
     Page page = mock(Page.class); 
    } 
} 

總的來說,最小的依賴性似乎是:

<dependency> 
    <groupId>com.day.cq.wcm</groupId> 
    <artifactId>cq-wcm-api</artifactId> 
    <version>5.5.0</version> 
    <scope>provided</scope> 
</dependency> 

<dependency> 
    <groupId>org.apache.sling</groupId> 
    <artifactId>org.apache.sling.api</artifactId> 
    <version>2.2.4</version> 
    <scope>provided</scope> 
</dependency> 

<dependency> 
    <groupId>com.day.cq</groupId> 
    <artifactId>cq-commons</artifactId> 
    <version>5.5.0</version> 
    <scope>provided</scope> 
</dependency> 

<dependency> 
    <groupId>com.day.cq</groupId> 
    <artifactId>cq-tagging</artifactId> 
    <version>5.5.0</version> 
    <scope>provided</scope> 
</dependency> 

但如果你是失敗的標記,然後你可能已經得到你需要的一切。

+1

感謝Diffa,它似乎是版本不匹配的問題。目前我在5.6.1上,也提到依賴版本是相同的,但這個版本並沒有這個jar版本。現在我已經將它更改爲更高版本,並且它一直適用於我。謝謝你快速的回覆。 –

+0

我把鏈接放在依賴查找器'http:// localhost:4502/system/console/depfinder'中,這樣你就可以找到正確的版本。這是一個非常有用的工具。 – diffa

+0

是的,它似乎是一個非常有用的鏈接tome。感謝分享這個。它是一個非常方便的功能。 –

相關問題