Tavian Barnes
Tavian Barnes

Reputation: 12922

How can I run unit tests with Maven as if they are in a JAR?

We recently had a bug caused by some code assuming that a classpath resource could be opened as if it were a regular file. This code was covered by unit tests which passed, because that assumption happens to hold both in Maven and Eclipse. But in production, that code was in a JAR in the service's WAR, and obviously didn't work.

We fixed the bug but I'm not quite satisfied because I can't see how to ensure that it doesn't happen again.

Upvotes: 2

Views: 71

Answers (1)

Daniel Kaplan
Daniel Kaplan

Reputation: 67360

Unit tests are run using Surefire. This happens during mvn test and "test" occurs before packaging. But if you want to run tests after mvn package, you should use Failsafe.

Here's some very relevant documentation about configuring the classpath. By default, it will put ${project.build.outputDirectory} in the classpath, but you should be able to prevent that by setting classesDirectory to some other directory. Then you can add your own jar as either an additionalClasspathElements or perhaps a dependenciesToScan

Upvotes: 3

Related Questions