Tuesday, October 23, 2012

Create your own maven integration test plugin

Did you ever user maven tomcat plugin to perform your integration test. Start your embedded tomcat server and do the integration test and then shut down the server. There is another popular cargo project doing the similar things.

What if you want to develop your own integration test with your minimal dependency and code effort. This article may be a place for you to start.

Generate the plugin project by using maven plugin archetype

mvn archetype:create-from-project \
-DartifactId=echo-maven-plugin \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-mojo

This maven archetype will generate a project with a default mojo, that we can
start development with. In this example I am going to use a socket echo server to demo the integration-test.

The Mojo which is an interface with a method execute() is the core execution path for maven plugins. Basically each goal attached with a Mojo. And the Mojo class can be annotated to express the goals, phases and properties.

Here is our start goal mojo StartMojo.java

The annotation of @goal tells maven which Mojo will be used during execution. The @phase will tell maven default phase to execute if no explicit phase configured.

This class will run the Echo Server in a backend thread listening on a server
socket. Then we create a StopMojo.java



This mojo has a stop goal and will shut down the echo server.
Similarly, we can have a run goal in case we want the echo server to run in the main execution rather than the backend thread. So it will block the maven execution for manual testing.




how are we going to use the plugin? We need to build the plugin by "maven clean install" to install the plugin into the local repository.

Now we can use the plugin to run the integration test. I created another maven project called integration-test. Here is how we configure the plugin usage



Then we create the integration test code. IntegrationTest.java



We need to exclude the test during the test lifecycle, since during the "test" life cycle the server is not up ready (because we start the server at pre-integration-test life cycle, which is after "test" life cycle happening).


Then we will include the test in integration-test lifecycle by using the fail-safe plugin. The reason we are using the fail-safe plugin for integration test is to make sure the build continue when the test failed, so the post-integration-test is triggered and the server can be elegantly shutdown.



If you want a standalone server running triggered by your maven plugin. Just run "maven echo:run", then the socket is running and waiting for connection.

This above example source code can be found here:
Source Code

References:
Guide of maven plugin development

Maven fail safe plugin

Maven life cycle

mojo-api-specification






















2 comments:

  1. Hello Shaozhen,
    The Article on Create your own maven integration test plugin is informative. It gives detailed information about it .Thanks for Sharing the information on Integration test Plugin Software Testing Company

    ReplyDelete
  2. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Visit here for Penetration testing services

    ReplyDelete