Guide to using attached tests

You can reuse the tests that you have created for one project in another. For example, suppose foo-core contains test code in the ${project.basedir}/src/test/java. To package up those compiled tests in a JAR and deploy them for general reuse, configure the maven-jar-plugin as follows:


<project>
  <build>
    <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-jar-plugin</artifactId>
       <version>3.0.2</version>
       <executions>
         <execution>
           <goals>
             <goal>test-jar</goal>
           </goals>
         </execution>
       </executions>
     </plugin>
    </plugins>
  </build>
</project>

The attached test JAR can be installed and deployed like any other Maven artifact.

To use the attached test JAR, specify a dependency on the main artifact with a specified type of test-jar and the classifier.


<project>
  ...
  <dependencies>
    <dependency>
      <groupId>com.myco.app</groupId>
      <artifactId>foo-core</artifactId>
      <version>1.0-SNAPSHOT</version>
      <classifier>tests</classifier>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>

    <dependency>
      <!--
        only needed if the test provider is not correctly autodetected by surefire
        ... and you are using junit-jupiter (othewise, replace with your test framework)
      -->
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>${junit.jupiter.version}</version> <!-- remember to set version as needed -->
      <scope>test</scope>
    </dependency>
  </dependencies>
  ...
  <build>
    <plugins>
      ...
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>${surefire.version}</version> <!-- remember to set version as needed -->
          <configuration>
              <dependenciesToScan>
                  <dependency>com.myco.app:foo-core:test-jar:tests</dependency>
              </dependenciesToScan>
          </configuration>
      </plugin>
      ...
    </plugins>
   </build>
   ...
</project>