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

I believe strongly in testing. I saw companies and products without any automatic test frameworks and the slightest change to the codebase (often in the urgent ‘we-need-to-fix-it-now-and-give-to-the-customer-in-1hr’ mode) made parts of the product or modules prone to crashes because no one could test the changes against the complete application. In my current endevour to create a new product we embrace testing, though we dont practice TDD (test-driven-development), but at least we maximize the amount of automatic testing with tools/frameworks like Netbeans, Hudson, Cobertura and others. If you dont practice TDD, how can you judge how much of your codebase is really tested, even you assume all you business testcases are covered ?

The other day Code Coverage crossed my way (Thanks Chris!). Eager to find out how to “do it” or to “use it” I found a few tools suitable for the Netbeans environment. It actually drilled down to only one tool: Cobertura. It fits exactly into our landscape using Netbeans and Hudson and it is opensource.
To get started with Cobertura you should be familiar (a bit) with Netbeans, JUnit and Ant. The Cobertura website offers sufficient reference for all functions and introduction to get you started. But what I missed is a tutorial the start from the scratch using Netbeans and Cobertura. I found 3 references (I will add them at the end,) which are not complete or just not working for me), so let me share the most basic getting-started steps with you.
Please note: This is how I get started, it might not be complete, foolproof or even correct in all details. Please feel free to comment, correct or give other feedback !

Part 1 (covering project creation and JUnit test)

Preparation:

  • You have basic knowledge of Java and Netbeans.
  • Netbeans 6.5.1 (I guess older or newer versions work also)

Tutorial:

  • Create a new Java Project using the new project wizard of Netbeans
    Call it CoberturaDemo. Deselect create Main class.

    Cobertura Tutorial

    Cobertura Tutorial

    Cobertura Tutorial

    Cobertura Tutorial

  • Create a new class
    Name it TestMe in the package demo

    Cobertura Tutorial

    Cobertura Tutorial

  • Add some basic functionality that we can test later.
    It is only a function that checks an int value if smaller than 5 or even in 2 if {} statements and returns 0 or 2. Useless function but we have something to “code-cover” !

    Cobertura Tutorial

    Cobertura Tutorial

    package demo;
    
    public class TestMe {
    
     public int doSomething(int i) {
     if (i < 5) {
     System.out.println("Smaller than 5");
     return 0;
     }
    
     if (i % 2 == 0) {
     System.out.println("Even number");
     return 2;
     }
    
     return -1;
     }
    }
    
  • Add JUnit Test Skeleton for our class
    By right-click the TestMe.java file in the demo package. Tools | Create JUnit test.
    For this tutorial we leave the defaults since we are not discussing them.
    Netbeans will create a test skeleton file for you (TestMeTest.java)..
    Comment: TestMe.java was not a good name choice. All Junit Test files are called {yourClassName}Test.java.

    Cobertura Tutorial

    Cobertura Tutorial

    Cobertura Tutorial

    Cobertura Tutorial

  • Create a real JUnit test
    The skeleton does NOTHING for us, so we need to add a test. You believe Netbeans creates testcases for you ?
    We create a simple test, only 1 for now, that throws a value at the method and checks for the expected value.

    Cobertura Tutorial

    Cobertura Tutorial

    package demo;
    
    import org.junit.Test;
    import static org.junit.Assert.*;
    
    public class TestMeTest {
    
     public TestMeTest() {
     }
    
     @Test
     public void testApp() {
     TestMe tst = new TestMe();
     int res = tst.doSomething(3);
     assertEquals("Correct value", 0, res);
     }
    }
    
  • Execute JUnit test
    We are already at out first testing stage ! Note that java class cannot be run, but the JUnit test.
    Right click on the project (project explorer) and select Test.
    You should get something like this:

    Cobertura Tutorial

    Cobertura Tutorial

    You can experiment, aka break it, by changing the expected value to 99 and execute test again.

Resume Part 1:
We crceate a Java class and a JUnit test class for it. We executed the test and saw “test passed”. We could belive “my class is tested ! lets move on..“.
NO ! Half the code is NOT tested and JUnit wont tell us either ! Of course it is obvious for this most simple sample, but just think about your projects that are usually a bit more complex. Would you “see” it, if not everything is covered ? Certainly NOT. So we need a tool to tell us.

Part 2 will introduce Cobertura as our new magic wizard to our demo project.
Part 3 will cover Hudson and the Cobertura plugin for Hudson.

Stay tuned.

Advertisements

5 thoughts on “Tutorial: Getting started with Netbeans and JUnit plus Cobertura (Part 1)

  1. Pingback: Tutorial: Getting started with Netbeans and JUnit plus Cobertura (Part 2) « The JavaDude Weblog

  2. Pingback: Tutorial: Getting started with Netbeans and JUnit plus Cobertura and Hudson (Part 3) « The JavaDude Weblog

  3. Thank you very much for sharing this tutorial with us. I am newbie in JUnit and your tutorial quickly helped me to understand how it works in Netbeans.

  4. Pingback: Code coverage tool for your Netbeans Java projects (how to install Cobertura, how to use Cobertura in Netbeans) « phpcoderblog

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