Search code examples
javamavenmaven-surefire-plugin

How do I execute Junit test case after package phase in maven?


I want to run my Junit on my build jar's, but maven surefire plugin executed on class file before package phase that is at test phase, is there any way to run all junit after building a jar of project. Reason to run on build jar is to verify obfuscation of jar.


Solution

  • Solution is to use the maven-failsafe-plugin which will in the integration test phase. You need to this to your pom file:

    <project>
      [...]
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
            <executions>
              <execution>
                <goals>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      [...]
    </project>
    

    You should following the conventions and follow the naming conventions of integration tests:

    <includes>
     <include>**/IT*.java</include>
     <include>**/*IT.java</include>
     <include>**/*ITCase.java</include>
    </includes>
    

    Afterwards you can run those integration tests via:

    mvn clean verify
    

    If you like to run only unit tests:

    mvn clean test