Search code examples
mavenintegration-testingmaven-plugin

How do I run a maven plugin's integration tests?


I've generated a maven plugin project using the maven-archetype-plugin archetype.

I've altered the generated integration test src/it/simple-it/verify.groovy to fail:

    assert false

I then invoked:

$ mvn clean install invoker:integration-test invoker:verify

And my assert false test passed:

[INFO] Installing /home/peter/ownCloud/Personal/eclipse-workspace/my-plugin/target/my-plugin-1.0-SNAPSHOT.jar to /home/peter/.m2/repository/org/example/my-plugin/1.0-SNAPSHOT/my-plugin-1.0-SNAPSHOT.jar
[INFO] Installing /home/peter/ownCloud/Personal/eclipse-workspace/my-plugin/pom.xml to /home/peter/.m2/repository/org/example/my-plugin/1.0-SNAPSHOT/my-plugin-1.0-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-invoker-plugin:3.1.0:integration-test (default-cli) @ my-plugin ---
[INFO] Building: simple-it/pom.xml
[INFO]           simple-it/pom.xml ................................ SUCCESS (13.4 s)
[INFO] 
[INFO] --- maven-invoker-plugin:3.1.0:verify (default-cli) @ my-plugin ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO]   Passed: 1, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.975 s
[INFO] Finished at: 2019-03-03T08:23:34-04:00
[INFO] Final Memory: 38M/441M
[INFO] ------------------------------------------------------------------------

Clearly the test wasn't actually invoked. What am I missing?

To demonstrate the issue, I've created a simple shell script:

#!/bin/bash

rm -rf my-plugin
mvn archetype:generate -DgroupId=org.example -DartifactId=my-plugin -DarchetypeArtifactId=maven-archetype-plugin -DarchetypeVersion=1.4 -DinteractiveMode=false
cd my-plugin
echo assert false > src/it/simple-it/verify.groovy
mvn clean install invoker:integration-test invoker:verify

I believe that this script should fail on the last line, when the invoker:verify goal is reached, and maven executes verify.groovy which is literally an assert false.


Solution

  • The archetype generator is creating a separate profile for integration tests which is disabled by default. You will need to enable this profile by running:

    mvn clean verify -Prun-its
    

    Then your test will fail.

    You can modify your POM to enable this profile by default by adding the following:

    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    

    The full profiles block would then be:

    <profiles>
        <profile>
            <id>run-its</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-invoker-plugin</artifactId>
                        <version>3.1.0</version>
                        <configuration>
                            <debug>true</debug>
                            <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
                            <pomIncludes>
                                <pomInclude>*/pom.xml</pomInclude>
                            </pomIncludes>
                            <postBuildHookScript>verify</postBuildHookScript>
                            <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
                            <settingsFile>src/it/settings.xml</settingsFile>
                            <goals>
                                <goal>clean</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </configuration>
                        <executions>
                            <execution>
                                <id>integration-test</id>
                                <goals>
                                    <goal>install</goal>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    At this point you will only need to run:

    mvn clean verify
    

    and it will also fail since the IT tests are now run by default.

    For more information about Maven build profiles have a look Here.