简体   繁体   English

在Adobe CQ5 CRXDE中编译引用Felix OSGI捆绑JUnit代码的单元测试

[英]Compile unit tests in Adobe CQ5 CRXDE that reference Felix OSGI bundle JUnit code

I want to write some unit tests that run within Adobe CQ 5.4. 我想编写一些在Adobe CQ 5.4中运行的单元测试。 I am doing what is described in this article for testing within CQ: 我正在做本文中描述的要在CQ中进行测试的内容:

http://jtoee.com/2011/09/799/ http://jtoee.com/2011/09/799/

However, after I create the unit test class in my Java code, it won't compile within CRXDE because it can't resolve the org.junit namespaces. 但是,在用Java代码创建单元测试类之后,由于无法解析org.junit名称空间,因此无法在CRXDE中进行编译。 I installed and activated the JUnit bundle in Felix as described (Apache Sling JUnit Core), but I am guessing there is something else I need to do in order for this active Felix bundle to be found in CRXDE. 我已按照描述(Apache Sling JUnit Core)在Felix中安装并激活了JUnit捆绑软件,但我想我还需要做些其他事情才能在CRXDE中找到此活动的Felix捆绑软件。 The Felix bundle in the CQ5 instance I am connected to shows these exported packages: 我连接到的CQ5实例中的Felix捆绑包显示了这些导出的包:

junit.framework,version=4.8.2
org.apache.sling.junit,version=1.0.7.SNAPSHOT
org.apache.sling.junit.annotations,version=1.0.7.SNAPSHOT
org.junit,version=4.8.2
org.junit.matchers,version=4.8.2
org.junit.rules,version=4.8.2
org.junit.runner,version=4.8.2
org.junit.runner.manipulation,version=4.8.2
org.junit.runner.notification,version=4.8.2
org.junit.runners,version=4.8.2
org.junit.runners.model,version=4.8.2

In this sample unit test code below, the last three import statements "cannot be resolved." 在下面的此示例单元测试代码中,最后三个导入语句“无法解析”。

import org.apache.sling.api.resource.*;
import org.junit.*;
import org.junit.runner.*;
import org.apache.sling.junit.annotations.*;

@RunWith(SlingAnnotationsTestRunner.class)
public class MyUnitTest {

    public ResourceResolver getResourceResolver() {
        try {
            return getResourceResolverFactory().
                    getAdministrativeResourceResolver(null);
        } catch (LoginException e) {
            fail(e.toString());
        }
        return null;
    }
}

It is my novice understanding that the OSGI bundle installed in Felix should be accessible for me to reference in my Java classes using CRXDE, but it isn't happening for the JUnit bundle I installed. 我的新手理解是,应该可以使用CRXDE访问在Felix中安装的OSGI捆绑包,以供我在Java类中进行引用,但是对于我安装的JUnit捆绑包却没有发生。 Why not? 为什么不? What do I need to do to get CRXDE to find the OSGI bundle reference and compile within CRXDE? 我需要怎么做才能使CRXDE找到OSGI包引用并在CRXDE中进行编译?

What you're doing looks correct at first sight. 乍一看,您正在做的事情看起来正确。

Did you try restarting CQ after installing the required bundles? 安装所需的捆绑软件后,您是否尝试过重新启动CQ? In theory that should not be required but I'm wondering if the bundle compiler is picking up the newly available packages correctly. 从理论上讲,这不是必需的,但我想知道捆绑编译器是否正确地选择了新可用的软件包。

I have uploaded a content package with a similar simple example at http://dl.dropbox.com/u/715349/cq5-examples/junit-tests-1.0.zip (md5 2915123ad581aa225bd531247ea02878), after installing this package on a fresh CQ 5.4 instance the example test is correctly executed via http://localhost:4502/system/sling/junit/ 在新的CQ上安装了内容包之后,我已经在http://dl.dropbox.com/u/715349/cq5-examples/junit-tests-1.0.zip(md5 2915123ad581aa225bd531247ea02878)上上传了带有类似简单示例的内容包。 5.4实例通过http:// localhost:4502 / system / sling / junit /正确执行了示例测试

You might want to try my sample and compare with yours. 您可能想尝试一下我的样品并与您的样品进行比较。

Short Answer 简短答案

The problem is not with CQ, the problem is with CRXDE. 问题不在于CQ,问题在于CRXDE。 CRXDE automatically downloads and caches required jar files on your local machine so they don't have to be retrieved constantly from CQ. CRXDE自动在本地计算机上下载并缓存所需的jar文件,因此不必从CQ不断检索它们。

If you switch to the 'Package Explore' navigation and then expand the project '{SERVER} {PORT} {HASH}' you should see a folder called Referenced Libraries. 如果切换到“打包浏览”导航,然后展开项目“ {SERVER} {PORT} {HASH}”,则应该看到一个名为“引用的库”的文件夹。 Right click and select Build Path >> Configure Build Path. 右键单击并选择构建路径>>配置构建路径。 From there you can add any dependencies you want into the project. 在这里,您可以将所需的任何依赖项添加到项目中。

Long Answer 长答案

CRXDE is not a good tool for creating bundles. CRXDE不是用于创建包的好工具。 It is much better to create bundles through a full fledged IDE such as Eclipse and utilize Apache Maven as a build tool. 最好通过成熟的IDE(例如Eclipse)创建捆绑包,并将Apache Maven用作构建工具。 Apache Maven can automatically manage your dependencies, run tests on your code and separate test vs. runtime dependencies. Apache Maven可以自动管理您的依赖关系,在您的代码上运行测试,以及将测试和运行时的依赖关系分开。

That way you can avoid having to load dependencies that you don't really need such a jUnit into your OSGi console and you have more control over how your bundle is built and deployed. 这样,您就可以避免将不需要的jUnit依赖项加载到OSGi控制台中,并且可以更好地控制捆绑包的构建和部署方式。

Day has a really nice guide to getting you set up with building CQ projects with Eclipse. Day提供了一个非常好的指南,可帮助您建立使用Eclipse构建CQ项目的设置。 http://dev.day.com/docs/v5_2/html-resources/cq5_guide_developer/ch04s02.html http://dev.day.com/docs/v5_2/html-resources/cq5_guide_developer/ch04s02.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM