Running Maven unit tests using the classpath method set for the generated JAR project

Some unit tests in my application are related to finding and managing some file resources that are part of the application itself. I need these tests to run in the actual production settings of the application, where it was deployed as a JAR file, and not as an exploded directory.

How can I instruct Maven to run my unit tests, considering the jar file (and any other declared library dependencies) created by the project as the class path instead of the compiled classes on the file system, as it happens by default?

In other words, right now the class path for my unit tests: /$PROJECTPATH/target/classes/ .

Instead, I would like this classpath to be set: /$PROJECTPATH/target/myjarfile.jar .

I tried to manually add and remove the dependency classes, as described here: http://maven.apache.org/plugins/maven-surefire-plugin/examples/configuring-classpath.html , but it still does not work.

My current POM project is as follows:

 <?xml version="1.0" encoding="UTF-8"?> <project> <modelVersion>4.0.0</modelVersion> <groupId>org.mygroupid</groupId> <artifactId>myartifact</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> ... </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.2</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.4</version> <configuration> <includeScope>runtime</includeScope> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> <executions> <execution> <id>copy-dependencies</id> <phase>process-resources</phase> <!-- <phase>package</phase> --> <goals> <goal>copy-dependencies</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.3</version> <configuration> <classpathDependencyExcludes> <classpathDependencyExclude> ${project.build.outputDirectory} </classpathDependencyExclude> </classpathDependencyExcludes> <additionalClasspathElements> <additionalClasspathElement> ${project.build.directory}/${project.build.finalName}.${project.packaging} </additionalClasspathElement> </additionalClasspathElements> </configuration> </plugin> </plugins> </build> </project> 

Thanks in advance for your help!

+4
source share
1 answer

Standard unit tests performed as part of the test life cycle phase cannot see the JAR project because the test phase runs before the package phase, so your tests run before Maven generates the JAR. See this page for a list of life cycle phases and their order.

What do you want it to run your tests as integration tests that run during the integration-test phase.

There are a number of guidelines for setting up Maven to run integration tests. Here and here are a couple of starters. a fail-safe plugin is commonly used to run integration tests.

I can’t remember exactly if the integration tests use target/classes or your project JAR file in the classpath. But if this is not the case, you can always create another Maven project, add your tests there and add the main project depending on this integration project. In some cases, this may be preferable to using the integration testing phase in the main project, if it is not just a standard Java library, for example, if you are writing an annotation handler.

+4
source

All Articles