Mockito#mock causes NoClassDefFoundException in OSGi

Today I migrated some of our OSGi integration tests in our bnd workspace to the OSGi-Test support. We already used our own wrapper test classes, to deal with services, service availability and configurations. With the great initial work and some support of a good friend, most of our existing features found their way into that test support for JUnit 5 and AssertJ.

It is really worth to take look into it! I started with the examples that are provided in the GitHub Repository. Because we test some business logic of certain components, I usually mock some dependent services and register them using the OSGi service ranks.

At a certain point I wanted to mock an interface that extends other interfaces. All the buildpath dependencies have been defined. But I ended up with the ugly NoClassDefFoundException. The problematic class was an import of one of the dependent interfaces.

Because my integration test project didn’t had any Import-Package declarations for that package, Mockito was obviously not able to find the class, because of the missing import. In that case bnd was not able to calculate these imports, because there is no direct usage of this package in the tests.

So I defined:

DynamicImport-Package: *

In the bnd.bnd, resp. the Manifest of the test bundle.

That fixed the problem!

by Mark Hoffmann