Continuous testing: using Hudson for Maven projects

December 27, 2008 at 11:42 pm 5 comments

I experimented a bit with Continuous Integration a while ago. I found some great freely available tools which facilitate the automatic building and testing of (web) applications. In this post I’ll discuss the setup of a system that uses Hudson to automate the build and testing process. I intend to write some follow up posts that digg further into testing a web application using Selenium

In this post I assume you have already set up a Maven project, and that you use Subversion as version control system (check out my other post if you are unsure about this). 

Configuring your project in Hudson

Hudson is a continuous integration engine, which means you can automate the building and testing of your sourcecode. A quickstart explaining continuous integration and Hudson is available in this presentation by Carlo Bonamico. 

Hudson comes as a .war container which you can either launch from the command line, or deploy in a servlet container like Tomcat. All is explained at the end of this page. If you use Tomcat, just put the .war file in [tomcat_folder]\webapps and start Tomcat. You can open hudson by surfing to [your_server]:8080\hudson. 

Adding a Maven2 project to Hudson is a piece of cake. Hudson is able to get the sourcecode from Subversion and execute Maven goals (clean, test, integration-test, …) on your project. 

 

hudson and svn

 

In Hudson, click on “new job” and select “maven2 project”. Fill in your project name and some description. Select the JDK to use for this job and enter the Svn repository URL. 

mavenjob1

Select a Maven goal to execute when this job is launched (e.g. deploy). That’s it, your Hudson job is created! You can now try building the project. You can check the console output in Hudson. By changing the Maven build lifecycle you can make the behavior of the Hudson job as complex as you like.

Automating the test and build

You probably noticed this while configuring the Hudson job: you can setup triggers to execute your job. The most interesting trigger in our case is a commit of new code to Subversion. Just enter 5 stars in the planning text field, so the subversion is checked for updates every minute. Change something in your code and commit this to Subversion, Hudson should start executing your job after about a minute.

trigger

Now you can add unit tests (e.g. in JUnit) to your project which test whether your code works as expected. Next add the maven-surefire-plugin to your Maven pom file. This plugin will execute your unit tests when the test goal is executed. A report will be generated that details the success or failure of all the tests. If the test goal is executed in your Hudson job, Hudson will automatically interpret the Surefire results and show them inside the Hudson interface. If you commit code to subversion and the unit tests fail, it will be noticed immediately. This allows to locate the source of the code breakage more easily. The biggest challenge won’t be locating this source of all evil, but determining a suitable punishment for the unlucky developper :-).

Creating nice reports

There exist some cool plugins for Maven to create reports about the state of your project. I won’t go into detail about all these plugins, but just point you to the correct location for more information about them (in most cases all you have to do is add the plugin to your <reporting> tag inside the pom file). These are the plugins:

  • Surefire plugin to create unit test reports
  • The Cobertura plugin will report the percentage of your code that is covered by unit tests.
  • Checkstyle will analyze your sourcecode and locate bad practices
  • CPD will analyze your sourcecode and locate copy-and-pasted code
  • PMD and Findbugs will analyze your sourecode and they will try to locate possible bugs

When executing the Maven site goal, a Maven site is generated which links to all these reports. If all goes well Hudson should link to this Maven site once it is created. 

The generation of a Maven site will take some time (several minutes) so you might want to create a seperate Hudson job for this. 

That’s it for this post. Hudson is also able to handle multi-module Maven projects (just add the parent project when configuring your job). It is possible to create an aggregated report that contains the results of all child projects, but this is quite tricky, so this is something for another post :p.

Advertisements

Entry filed under: java, Uncategorized. Tags: , , , , , .

Two of the best free applications out there Weekend webcam project: playing with the reacTIVision framework

5 Comments Add your own

  • […] Continuous testing: using Hudson for Maven projects « Kr3l’s Turf […]

    Reply
  • 2. Freddy Mallet  |  December 30, 2008 at 10:53 pm

    Hi Carel,

    If you’re interested by aggregated all surfire, coberture, pmd, checkstyle, findbugs reports to get a synthetic view on your projects quality, you can take a look to Sonar. Sonar also comes along with a Hudson plugin. To get a quick insight, go to Nemo, public instance of Sonar, dedicated to analyze a bunch of open source projects.
    regards
    Freddy

    Reply
  • 3. Karel  |  December 31, 2008 at 8:28 am

    Hey Freddy,

    Thanks for the comment. I used the dashboard plugin to create the aggregated reports, which required some trial and error (because it required multiple maven goals to be executed one by one) but everything works now. I will probably write some short post about using this plugin together with Hudson.

    Sonar is indeed a very nice alternative to create the aggregated reports, though I haven’t got any experience with it. Thanks for the links!

    Regards,
    Karel

    Reply
  • […] A Jenkins/Hudson instance for your continuous integration runs. For setting up hudson you can refer here […]

    Reply
  • 5. Continuous Integration with Selenium | AutomationChinnu  |  August 5, 2015 at 2:14 pm

    […] Jenkins/Hudson instance for your continuous integration runs. For setting up hudson you can refer here […]

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Feeds

Articles to be written…

Twitter – kr3l

my del.icio.us

RSS Google Reader Shared Stuff

  • An error has occurred; the feed is probably down. Try again later.

RSS Listening to..

  • An error has occurred; the feed is probably down. Try again later.

%d bloggers like this: