Integration testing

In the classic software lifecycle, unit tests are naturally followed by the integration phase. Earlier, we studied the unit testing of a Maven plugin, and now we will deepen the integration phase.

The integration phase is covered by maven-invoker-plugin because this plugin can run a set of Maven projects (features that we didn't use at all for our plugin project) and can verify the output generated from the project launched. The ability to verify the output generated from the project that is executed is accomplished by a script that could be a bash script or a groovy script. This plugin is included in a specific build profile for the integration phase.

Tip

The integration build profile, by default, is named run-its. It is possible to rename it.

Whenever we want to perform integration tests, we have to run the following command:

$ mvn integration-test -Prun-its

When we run such a command, Maven executes the plugin related to the profile, and creates a local repository structure in the ${basedir}/target directory. Once the local repository has been created, Maven tries to install the plugin under testing and verifies the correctness of the installation process. In order to perform the check for the correct installation of the plugin, we used the configuration option of maven-invoker-plugin: postbuildhookscript called verify.bsh.

The configuration part of the plugin is as follows:

<postBuildHookScript>verify</postBuildHookScript>

It is important to adopt a custom local repository for integration tests, such as the ${basedir}/target folder. Thus, we avoid polluting the local Maven repository.

As we can see from the configuration of maven-invoker-plugin, we configured the plugin in order to perform the installation, the integration test, and verify the goals:

<executions>
  <execution>
     <id>integration-test</id>
     <goals>
        <goal>install</goal>
        <goal>integration-test</goal>
        <goal>verify</goal>
     </goals>
  </execution>
</executions>

The structure created to perform tests is as follows:

Integration testing

The settings.xml file contains settings for maven-invoker-plugin. These settings allow Maven to locate the test repository.

The pom.xml files within the folders simulate the project environment in which we want to test our plugin.

We had two test projects to test two different conditions: it-test-1 verifies the correct plugin installation and it-test-2 verifies whether the database was updated correctly. All tests were performed by verify.bsh.

If scripts found no expected behavior, they throw an exception.

When we execute the integration-test command on the pom.xml file, we obtain the sequence of operations declared in maven-invoker-plugin:

[INFO] --------------------------------------------------------------
[INFO] Building mantis-plugin Maven Mojo 0.0.1-SNAPSHOT
[INFO] --------------------------------------------------------------
 [INFO] --- maven-invoker-plugin:1.7:install (integration-test) @ mantis-maven-plugin ---
[INFO] Installing projects/mantis-plugin/pom.xml to target/local-repo/com/example/mojo/mantis-maven-plugin/0.0.1-SNAPSHOT/mantis-maven-plugin-0.0.1-SNAPSHOT.pom
[INFO] Installing /Users/robertobaldiprojects/mantis-plugin/target/mantis-maven-plugin-0.0.1-SNAPSHOT.jar to /target/local-repo/com/example/mojo/mantis-maven-plugin/0.0.1-SNAPSHOT/mantis-maven-plugin-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-invoker-plugin:1.7:integration-test (integration-test) @ mantis-maven-plugin ---
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO] Building: it-test-1/pom.xml
[INFO] ..SUCCESS (5.3 s)
[INFO] Building: it-test-2/pom.xml
[INFO] run script verify.bsh
[INFO] ..SUCCESS (3.4 s)
[INFO] --------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------

As we can see from the preceding logs (the only interesting lines were reported for shortness), first the plugin is installed. Then, Maven finds the two test projects and executes our plugin on the projects. After that, it executes postbuildhookscript in order to check the correct installation of the plugin (test-1) and the correct execution of the plugin on the database (test-2).

If something goes wrong during one of the checks, then the following error is returned:

[INFO] Building: it-test-1/pom.xml
[INFO] ..SUCCESS (4.3 s)
[INFO] Building: it-test-2/pom.xml
[INFO] run script verify.bsh
[INFO] ..FAILED (3.5 s)
[INFO]   The post-build script did not succeed. Database is not updated correctly!! 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset