2

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:

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

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. Why not? What do I need to do to get CRXDE to find the OSGI bundle reference and compile within CRXDE?

Shawn
  • 8,374
  • 5
  • 37
  • 60
  • Hey Shawn, I am trying to create the same test setup. Did you have any success with any of the solutions yet? Thx a lot for any help. Can you point me to some decent info? – Denis Apr 03 '12 at 14:18
  • I haven't found an answer yet as to why installing the bundle into Felix isn't enough for CRXDE to resolve the references. In the end what I did was to essentially re-reference the jar files that are installed in Felix from within my code. I created a separate bundle in my source code to contain my unit tests, and I added a "libs" folder at the same level as my src folder and put the junit-4.10.jar and org.apache.sling.junit.core-1.0.7-SNAPSHOT.jar there so my code could resolve the references. It is similar to what Bertrand did...I just don't understand why re-referencing is necessary yet. – Shawn Apr 03 '12 at 18:56

2 Answers2

2

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

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

Bertrand Delacretaz
  • 6,100
  • 19
  • 24
  • Thanks for putting this together! I tried restarting CQ after installing/activating the Apache Sling JUnit Core bundle, but I still was unable to compile my test class. I looked at this sample code, and the biggest thing I can see is that the "install" directory at apps/junit-tests/install that contains two apparently manually-added .jar files (in addition to the dynamically-added com.example.junit-tests.jar): junit-4.8.2.jar, org.apache.sling.junit.core-1.0.6.jar. If I've already installed the JUnit bundle into Felix, do I really need to include these 2 extra .jars in my unit test bundle? – Shawn Jan 17 '12 at 16:22
  • The tests need those two extra bundles, so yes if they are not active yet (or if it's older versions that you have) you need to add them. – Bertrand Delacretaz Jun 21 '12 at 08:23
2

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. http://dev.day.com/docs/v5_2/html-resources/cq5_guide_developer/ch04s02.html

Dan
  • 364
  • 1
  • 2
  • I am able to add references the way you described, but why should I need to do this at all? I thought that by installing the bundle in Felix, CRXDE would be aware of it (reference it) when it loaded. That doesn't seem to be happening though. Even using this method to add a , I can't find the Felix bundles, but they end up getting added off my file system instead of out of the running CQ instance. All the libraries shown on my build path come from {SERVER}_{PORT}_{HASH} locations, but the stuff I add come from things like c:\dir instead. How do I locate the JUnit bundle I installed in Felix? – Shawn Jan 18 '12 at 19:58
  • This article (http://dev.day.com/docs/en/cq/current/developing/developmenttools.html#How%2520to%2520Set%2520Up%2520the%2520Development%2520Environment%2520with%2520Eclipse) is more recent than the one you linked to, and it says "CRXDE is recommended when you develop complex applications by creating new components and Java bundles." I know there are always alternative ways to do things, but for this question, I am asking in the context of using CRXDE. – Shawn Jan 18 '12 at 20:08