Unit Testing with JUnit 4 – First Sample Project

1. Let’s create our First JUnit Test Class

Go ahead and create a new Java class called ArraysTester.

For this example I will add in all the annotations we discussed previously in the last lesson.  Click here to go back for a refresher.

In this example, I am keeping it simple by only having outputs in many of the methods I created and in the one where actual tests will take place, most use an array of numbers and I call the static sort() method in the Arrays class.

ArraysTester.java

package com.omega.test;

import static org.junit.Assert.assertArrayEquals;

import java.util.Arrays;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class ArraysTester {
  private static int counter = 1;

  @BeforeClass
  public static void setupArraysTester() {
    // initialize the class to some known state
    System.out.println("Running setupArraysTester...n");
  }

  @AfterClass
  public static void teardownArraysTester() {
    // possible to close out some resources
    System.out.println("Running teardownArraysTester...");
  }

  @Before
  public void beforeTestSetup() {
    System.out.println("Running before test #" + counter);
  }

  @After
  public void afterTestSetup() {
    System.out.println("Running after test #" + counter);
    System.out.println("-------------------------------n");
    counter++;
  }

  @Ignore("Test is being ignored, skipping over it")
  @Test
  public void testNegativeValues() {
    int[] numbers = {-4,-11,-19,-3,-1,0};
    int[] expectedOutput = {-19,-11,-4,-3,-1,0};

    System.out.println("Running testNegativeValues()...");
    Arrays.sort(numbers);
    assertArrayEquals(expectedOutput, numbers);
  }

  @Test
  public void testArraysSort() {
    int[] numbers = {6,4,3,2,1,5,11,101,23,36,54,31};
    int[] expectedOutput = {1,2,3,4,5,6,11,23,31,36,54,101};

    System.out.println("Running testArraysSort()...");
    Arrays.sort(numbers);
    assertArrayEquals(expectedOutput, numbers);
  }

  @Test(expected=NullPointerException.class)
  public void testArraysSortwithNullValue() {
    int[] numbers = null;

    System.out.println("Running testArraysSortwithNullValue()...");
    Arrays.sort(numbers);
  }

  @Test(timeout=150)
  public void testArraysTimeout() {
    Random rnd = new Random();

    System.out.println("Running testArraysTimeout()...");
    for (int i = 0; i < 1200000; i++) {
      int[] numbers = {i, i+rnd.nextInt(100), i-rnd.nextInt(50)};
      Arrays.sort(numbers);
    }
  }
}

2. Running the JUnit test cases from Eclipse

Once this code is complete we will need to go back to the project explorer and put the highlight our class, in this case “ArraysTester”, right click and choose Run As->JUnit Test

junit_runas

3. JUnit Tab

Once this is done and the test cases have all executed.  Eclipse will display the status of the runs in a new tab called JUnit.

junit_run

Clicking on each of the test cases will take you to the appropriate place in the java code.  You can use this to see what went wrong in the tests that failed.

Other nice features that Eclipse has is that it will allow you to filter and show errors only or show all.   You will notice an icon filter_errors.   Clicking on this icon will toggle back and forth.

Now let’s modify our failed test such that the timeout error will disappear and we can  get a good test run. I am going to modify the timeout from 150ms to 3000ms. see below.

  @Test(timeout=3000)
  public void testArraysTimeout() {

Now let rerun the JUnit test and voila!   As you can see the previously failed tests actually completes in 216ms so with the timeout of 150ms limit, it would have failed the timeout tests.

junit_run1

4. Output

junit_console

As you can see from the output above, running our tests we can see the the setupArraysTester() method which has the @BeforeClass annotation only ran once, at the very start of the test.  Next we see that our beforeTestSetup() method gets run before every test it was preceded by the @Before annotation.   Next the actual test is run, and after it completes the afterTestSetup() method which contained the @After annotation is run.  This repeats for every method being tested and just before the test is over the teardownArraysTester() method is called which contained the @AfterClass annotation.

One more thing to note is that our testNegativeValues() method never got called as this one was left out with the @Ignore annotation.

Other Related Posts

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *