Ryuhei Irie
Ryuhei Irie

Reputation: 51

junit test code is not included in the jar

I am developing 3 projects, projectB and projectC refer to projectA. I am creating JUnit test code for each of projects A, B, and C. I want to put the common logic used in the tests in projectA, and projectsB and C refer to it.

However, the projectA.jar created by mvn install does not contain any test code.

How do I include test code in the jar?

Upvotes: 0

Views: 462

Answers (1)

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40318

This page has all the details, repeated here as per SO guidelines - BUT ALSO SEE "The Preferred Way" AT THE END:

  • The Maven Jar plugin has a test-jar goal, activate it as usual:

    <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>
    
  • This jar gets deployed with the other artifacts of the project, no extra configuration.

  • Use the following ceremony to declare as dependency in test scope of course:

    <dependency>
      <groupId>groupId</groupId>
      <artifactId>artifactId</artifactId>
      <classifier>tests</classifier>
      <type>test-jar</type>
      <version>version</version>
      <scope>test</scope>
    </dependency>
    

The preferred way

Create a separate project/Maven module that has all the reusable test code in the src/main/ folder, depend on that artifact in test scope.


I prefer the preferred way :)

Upvotes: 2

Related Questions