我正在使用Mockito框架来创建用于为我的Adobe CQ5项目编写junits的模拟对象。我有一些我想用JUnit测试的服务。
当我想使用下面的代码创建一个模拟Page'对象时,我发现了一个奇怪的问题,它给出了以下错误:
Page pageObj = Mockito.mock(Page.class);
但其他api classess / interfaces
并非如此 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类的情况下给我错误。
答案 0 :(得分: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>
虽然如果你没有标记,那么你可能已经得到了你需要的一切。