Testing Mojo

An essential need during development is to test the code. In order to satisfy this need, we used a specific library named maven-plugin-testing-harness. Such a library has been designed specifically to test Mojo functionalities.

To start using testing-harness for our tests, we need to create a test class in the test package. As in the Mojo development, our class must extend this abstract class in order to inherit testing and utility methods:

org.apache.maven.plugin.testing.AbstractMojoTestCase

As a first step, we must override the following two basic methods for initialization and ending:

@Override
protectedvoid setUp() throws Exception {
  super.setUp();
}

@Override
protectedvoid tearDown() throws Exception {
  super.tearDown();
}

The setUp method is in charge of creating a default Maven project configuration, and adding it to PlexusContainer. For IoC's tearDown method simply dismisses PlexusContainer.

Tip

PlexusContainer was chosen by the Maven team because at that time, it was the only implementation of the Inversion of Control (IoC) pattern.

Later on, Spring came out. Spring is a general framework encapsulating Inversion of Control using XML/Annotations and other patterns build scalable web applications.

All public methods with the prefix test will be executed during the test execution phase. To launch the test case, test, we can use both Eclipse or the Maven command line. Execute this command from the project's root directory:

$ mvn test

The inheritance hierarchy of the AbstractMojotestCase class refers to the junit.framework.TestCase class. This allows us to use all the Junit methods for assertions.

In order to test a plugin, we need a project context for fetching project properties. Since we can't load the test POM inside the test case, we must use the following method:

public static File getTestFile(final String path)

This method gets the POM file from the location passed as the input parameter.

In order to start our Mojo, we need to load it from the POM file loaded through the getTestFile method. We can easily perform this operation using the following inherited method:

protected Mojo lookupMojo(String goal, File pom) throws Exception

Otherwise, we can load a Mojo with a particular configuration using the following code:

protected Mojo lookupConfiguredMojo(MavenProject project, String goal) throws Exception

It is also possible to perform multiple launches to test different goals, as follows:

protected MojoExecution newMojoExecution(String goal)

We can also extract the plugin configuration using the following code:

protected PlexusConfiguration extractPluginConfiguration(String artifactId, File pom)throws Exception

The PlexusConfiguration object contains all the configuration within a map, so we can test whether some configuration is present or not and elaborate those values.

..................Content has been hidden....................

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