Getting ClassNotFound for PaxExam Option class when running test

1.1k views Asked by At

When running unit tests with PaxExam I'm getting this ClassNotFoundException:

java.lang.ClassNotFoundException: org.ops4j.pax.exam.Option
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2693)
at java.lang.Class.privateGetPublicMethods(Class.java:2894)
at java.lang.Class.privateGetPublicMethods(Class.java:2903)
at java.lang.Class.getMethods(Class.java:1607)
at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.findAndInvoke(JUnitProbeInvoker.java:94)
at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.call(JUnitProbeInvoker.java:73)
at org.ops4j.pax.swissbox.framework.RemoteFrameworkImpl.invokeMethodOnService(RemoteFrameworkImpl.java:433)
at org.ops4j.pax.swissbox.framework.RemoteFrameworkImpl.invokeMethodOnService(RemoteFrameworkImpl.java:406)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
at sun.rmi.transport.Transport$1.run(Transport.java:178)
at sun.rmi.transport.Transport$1.run(Transport.java:175)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:174)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:557)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:812)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:671)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

This happens even though the PaxExam JAR (and all dependencies) are on the Maven classpath.

I have been trying to hunt down a solution to this problem for quite some time; I would really appreciate any advice anyone can give. Thanks.

EDIT:

The error seems to arise from the fact that each test is implementing a interface BundleTest imported (dynamically) from another bundle that provides a default @Configuration method. This bundle does not (currently) explicitly export or import the pax exam classes, so that might be part of the problem?

I will update the question again once I have tested it to find out.

2

There are 2 answers

0
bgroenks On BEST ANSWER

The problem indeed seemed to be that the class was referencing via import another class that imported Pax classes.

To illustrate the imports:

class A <-- class B <-- Pax

The problem was that class B was not importing the Pax Exam classes in the Bundle manifest. So while Pax was creating the dynamic imports for the test classes in class A, class B was from another bundle. That meant OSGi wasn't resolving the imports in class B.

The lesson then, I suppose, is check the declared dependencies of your other dependent bundles.

Hopefully this helps someone else who has this problem in the future!

8
Neil Bartlett On

Your bundle needs to import the package org.ops4j.pax.exam, e.g. something like this in META-INF/MANIFEST.MF:

Import-Package: org.ops4j.pax.exam

Update: see this wiki page for further information.