Tutorial: Getting started with Netbeans and JUnit plus Cobertura and Hudson (Part 3)

Following part 1 and 2 which covered the JUnit test class creation and the coverage evaluation by Cobertura, we will add in part 3 another dimension: Automation. So far we run the tests manually, but we want to achieve a continuous integration and test environment.

Part 3 (Cobertura and Hudson)

Preparation:

  • Part 1 (link)
  • Part 2 (link)
  • Download Hudson (website)
  • Running Subversion with the project uploaded

Tutorial:

  • Download and install Hudson
    Hudson can run standalone (easier for testing, trialing and experimenting) or as a deployed application on an application server.
    Once you downloaded it, just navigate with a terminal session (aka Dos-Box) and start it with “java -jar hudson.war
    You will get something like this:

    Hudson

    Hudson

    I will not go into any details of how to configure Hudson, but jump straight to our project. Maybe in another tutorial I will explain the basic setup of Hudson, but it so simple and the documentation is so good, it is easy for your to do the necessary settings.

  • Tweak our Netbeans Project
    As per standard setting when creating a new Netbeans project, the libraries are not kept in the project folder, but copied for deployment only. But Hudson need to access these libraries, one approach to solve this, is to keep the library files in the project folder.

    Netbeans Project

    Netbeans Project (Standard Folder)

    SVH20090925_015

    Local Libraries

    Open the project in Netbeans and open the project properties (right click project name).  In the libraries section you just click Browse and Next.

    Libraries

    Libraries

    Once this is done the libraries are kept locally in your project folder.
    Now comes the interesting part, you remember the recommendation from the previous part of the tutorial saying you should modify only the build.xml file ?

    build-impl.xml

    build-impl.xml

    Netbeans needs to recreate its build file after our changes. So either we change it for Netbeans and select cancel (not knowing what changes need to be done) or we redo our changes or keep them finally in our own build.xml file.

  • Create new Job in Hudson
    Your Hudson is running and you can access it via http://localhost:8080 (assuming you run it as standalone on your local machine)
    You see a mainpage like this

    Hudson Portal

    Hudson Portal

    Create new Hudson Job (Build a free-style software project)

    Create new Hudson Job

    Create new Hudson Job

    Configuration of new Job in Hudson

    New Hudson Job

    New Hudson Job

    The following parts of the configuration page are interesting for us
    SVN repository

    SVN

    SVN

    Invoke Ant (please note it needs to be downloaded and configured first in the main Hudson configuration)

    Invoke Ant

    Invoke Ant

    Trigger JUnit Report Creation (default path is usually **/build/test/results/*.xml)

    JUnit Report

    JUnit Report

    Trigger Corbertura Report (default path is **/build/report/cobertura/coverage.xml)

    Trigger Cobertura Report

    Trigger Cobertura Report

    All other configuration we dont need right now (I added the email function to drop me an email if the build is broken).

  • Trigger the first Build
    Job Screen (no build yet)

    Job Screen (no build yet)

    First Build

    First Build

    If everything is running fine we should have result like this (inclusive all Ant and JUnit log output)

    ...
    BUILD SUCCESSFUL
    Total time: 10 seconds
    Recording test results
    Publishing Cobertura coverage report...
    Publishing Cobertura coverage results...
    Finished: SUCCESS
    

    Starting with the second build we will see a graph giving us tendencies over the last builds.
    In our sample project we have covered 100% of the classes but only 50% of the lines.

    kk

    Build Overview

    Test Code 50%
    @Test
    public void testApp() {
    TestMe tst = new TestMe();
    int res = tst.doSomething(3);
    assertEquals(“Correct value”, 0, res);

    //res = tst.doSomething(6);
    //assertEquals(“Correct value”, 2, res);

    //res = tst.doSomething(11);
    //assertEquals(“Correct value”, -1, res);
    }

    Lets re-insert the full test code and we get this after our next build:

    b

    Build Overview

Where to go from here:

  • Explore all the features of Hudson, eg. Javadoc feature, trigger other builds and use fingerprints.
  • Make Hudson production ready by deploying it on an application server such as Glassfish and securing it with logins,..

What else could I plug-in or do:

  • There is a huge number of plugins available that help you to integrate with all kind of tools and frameworks.
  • You can try the Netbeans Hudson feature (version 6.7 and higher)
  • You could try using the Firefox plugin to constantly see the build status.
About these ads

4 thoughts on “Tutorial: Getting started with Netbeans and JUnit plus Cobertura and Hudson (Part 3)

  1. Pingback: Tutorial: Most simple Test Application for Embedded Glassfish + Netbeans + Hudson « The JavaDude Weblog

  2. Hi , i am getting below error after hudson execution.

    Total time: 5 seconds
    Recording test results
    Archiving artifacts
    Publishing Cobertura coverage report…
    No coverage results were found using the pattern ‘**/build/report/cobertura/coverage.xml’ relative to ‘C:\Program Files\Hudson\jobs\junitTesting\workspace’. Did you enter a pattern relative to the correct directory? Did you generate the XML report(s) for Cobertura?
    Finished: FAILURE

  3. Now after resolving above issue , now i am getting below message :
    BUILD SUCCESSFUL
    Total time: 4 seconds
    Recording test results
    No test report files were found. Configuration error?
    Archiving artifacts
    Skipping Cobertura coverage report as build was not UNSTABLE or better …
    Finished: FAILURE

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