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

By : Shawn

I want to write some unit tests that run within Adobe CQ 5.4. I am doing what is described in this article for testing within CQ:

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. 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. The Felix bundle in the CQ5 instance I am connected to shows these exported packages:


In this sample unit test code below, the last three import statements "cannot be resolved."

import org.junit.*;
import org.junit.runner.*;

public class MyUnitTest {

    public ResourceResolver getResourceResolver() {
        try {
            return getResourceResolverFactory().
        } catch (LoginException e) {
        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. Why not? What do I need to do to get CRXDE to find the OSGI bundle reference and compile within CRXDE?

By : Shawn


What you're doing looks correct at first sight.

Did you try restarting CQ after installing the required bundles? 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 (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/

You might want to try my sample and compare with yours.

Short Answer

The problem is not with CQ, the problem is with CRXDE. CRXDE automatically downloads and caches required jar files on your local machine so they don't have to be retrieved constantly from 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. 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. It is much better to create bundles through a full fledged IDE such as Eclipse and utilize Apache Maven as a build tool. Apache Maven can automatically manage your dependencies, run tests on your code and separate test vs. runtime dependencies.

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.

Day has a really nice guide to getting you set up with building CQ projects with Eclipse.

By : Dan

This video can help you solving your question :)
By: admin