If you are testing your JEE application using Arquillian and running in to issues with ClassNotFoundExcpetions, the solution is pretty simple. To resolve the issue, simply add the class and/or package to your Deployment Descriptor in your test. This will add the classes to the Archive sent to the container when the tests run.

Adding a Local Package

Adding an entire package to your deployment archive is fairly simple as Arquillian provides a number of methods for you to use. The easiest was though is to tell your archive to add all the classes in given package and all of its sub-packages. This can be done by using

addPackages(true, "your.package.name");

Adding a Maven Dependency

Sometimes the offending classes will not be part of your code, but instead will come from one of your Maven dependencies. This generally happens when you are testing your UI and your code needs additional 3rd part libraries. Adding these Maven resources to your deployment archive is a snap. First you will need to create a Maven Dependency Resolver:

final MavenDependencyResolver resolver = DependencyResolvers.use(MavenDependencyResolver.class).loadMetadataFromPom("pom.xml");
  

Then you will need to add the required dependency to your archive by calling addAsLibraries, replacing the variables with the correct information for the dependency you want to add.

final WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war").addAsLibraries(resolver.artifact("<dep_groupid>:<dep_artifactid>:<dep_version>").resolveAsFiles());


Published

21 July 2012

Tags