Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JUnit Tests contained in dependency jar using Maven Surefire

I have a jar in my maven repository that contains junit tests, which should be run in different projects, because it is able to inspect the project and test for certain features of it. Unforunately surefire doesn't pick up tests that are contained in a jar, as this Feature Request shows.

In the feature request they propose to unpack the jar to be then executed by surefire.

I successfully unpacked the jar using the maven-dependency-plugin, but the contained tests are not executed anyway. This is how I configured the maven-dependency-plugin to unpack my jar:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-dependency-plugin</artifactId>     <executions>         <execution>             <id>unpack</id>             <phase>process-test-classes</phase>             <goals>                 <goal>unpack</goal>             </goals>             <configuration>                 <artifactItems>                     <artifactItem>                         <groupId>de.mwx.test</groupId>                         <artifactId>selenium-test-base</artifactId>                         <version>0.1</version>                         <overWrite>true</overWrite>                           <outputDirectory>                               ${project.build.directory}/classes                           </outputDirectory>                     </artifactItem>                 </artifactItems>             </configuration>         </execution>     </executions> </plugin> 

Any help would be appriciated.

like image 777
devsnd Avatar asked May 08 '12 10:05

devsnd


People also ask

How do you run a JUnit test case in Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

Which is the Maven plugin for running JUnit tests?

The JUnit Platform Provider supports the test JVM system property supported by the Maven Surefire Plugin. For example, to run only test methods in the org. example. MyTest test class you can execute mvn -Dtest=org.

Why your JUnit 5 tests are not running under Maven?

Prior to these releases, to run JUnit 5 tests under Maven, you needed to include a JUnit provider dependency for the Maven Surefire plugin. This is correct for pre 2.22. 0 releases of Maven Surefire/Failsafe.

How do I run a JUnit test in Maven project in Eclipse?

you can run Junit 4 with Maven. You just need the Junit 4 dependency in your pom. You also need the surefire plugin to execute the tests. Hint: By default surefire looks for files with *Test.


1 Answers

There is a way of running a test in maven from another jar. from maven-surefire-plugin version 2.15 you can tell maven to scan your test jars for tests and run them. You don't need to extract the tests jar. Just add a dependency to your test jar and:

<plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-surefire-plugin</artifactId>         <configuration>             <dependenciesToScan>                 <dependency>test.jar.group:test.jar.artifact.id</dependency>             </dependenciesToScan>         </configuration>     </plugin> 

Took this stuff from https://gist.github.com/aslakknutsen/4520226 And https://issues.apache.org/jira/browse/SUREFIRE-569

As expected, this works for JUnit and Testng. Will probably work for anything that surefire can run.

like image 84
galusben Avatar answered Oct 12 '22 11:10

galusben