Search code examples
mavenunit-testingintegration-testingsurefiremaven-failsafe-plugin

Testing content in POMs for multi-module Maven project


I have read here about unit testing with the Surefire plugin and integration testing with the Failsafe plugin, but I am still unclear about how the POMs should look in a Maven project that consists of a parent and multiple child modules, each with their own POM file.

Questions:

  • Has anyone implemented integration tests and unit tests in each of their modules?
  • If so, would you be so kind as to show your POMs so I have an example of a good working configuration?

Solution

  • See my answer to question: How can I switch between two test suites in Maven 2? I prefer maven module - very easy to implement and you do not need knowledge about other plugin.

    If you using you can just define in your parent pom (one place only):

    <profile>
        <id>normal</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <excludedGroups>integration</excludedGroups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        </profile>
    <profile>
        <id>integration</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <includedGroups>integration</includedGroups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    

    Annotate all integration tests by:

    @Test(groups="integration")
    

    If you use junit see Category

    You run normal test by: mvn clean install integration tests by mvn -Pintegration clean install