Search code examples
javamavenjunit4

How to deploy a war via maven before running junit4 integration tests?


I want the maven to deploy war before running the jUnit tests.

I have added the failsafe plugin and a configuration but the deploy is still done after the tests.

Are the tests not run by the failsafe plugin?

<build>
    <finalName>testWar</finalName>
    <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.16</version>
        <configuration>
            <systemPropertyVariables>
                <wildfly.remote.port>10090</wildfly.remote.port>
            </systemPropertyVariables>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>org.wildfly.plugins</groupId>
        <artifactId>wildfly-maven-plugin</artifactId>
        <version>1.2.1.Final</version>

        <configuration>
            <hostname>host</hostname>
            <port>10090</port>
            <username>user</username>
            <password>pw</password>
        </configuration>

        <executions>
            <execution>
                <id>wildfly-run</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>deploy</goal>
                </goals>

            </execution>
        </executions>
    </plugin>
</plugins>
</build>

Solution

  • The behaviour you report suggests that your tests are being run in the test phase instead of the integration-test phase. This is because by default both the Maven Surefire and Maven Failsafe plugins use class name conventions to differentiate between the two types of tests, as described below:

    Default Unit Test Behaviour

    The Maven Surefire Plugin runs unit tests in the test phase. By default it selects tests to execute based on the class name of the test, as described in Maven Surefire Plugin | Inclusions and Exclusions of Tests:

    • "**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
    • "**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
    • "**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
    • "**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".

    Default Integration Test Behaviour

    The Maven Failsafe Plugin runs integration tests tests in the integration-test phase. By default it also selects tests to execute based on the class name of the test, as described in [Maven Failsafe Plugin | Inclusions and Exclusions of Tests](https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html:

    • "**/IT*.java" - includes all of its subdirectories and all Java filenames that start with "IT".
    • "**/*IT.java" - includes all of its subdirectories and all Java filenames that end with "IT".
    • "**/*ITCase.java" - includes all of its subdirectories and all Java filenames that end with "ITCase".

    Therefore you probably just need to rename your integration test classes to conform with one of the three conventions shown above.