Java JUnit Tutorial - JUnit Parameterized Test








Junit 4 has introduced a new feature called Parameterized tests.

We can use Parameterized tests to run the same test using different values.

Example

The following code creates a business function which tells if an input integer is a prime number.

public class BusinessLogic {
  public Boolean validate(final Integer primeNumber) {
    for (int i = 2; i < (primeNumber / 2); i++) {
      if (primeNumber % i == 0) {
        return false;
      }//from   w w  w .ja v a 2  s.  c  o  m
    }
    return true;
  }
}




Test case

The following code shows how to create a Parameterized Test case.

In order to create a Parameterized Test case, we first need to mark the test case class as Parameterized by using the @RunWith annotation.

@RunWith(Parameterized.class)
public class TestJunit {

Then we prepare the parameters for each test cases.

The method which provides the parameters is marked with @Parameterized.Parameters.

     @Parameterized.Parameters
     public static Collection primeNumbers() {
       System.out.println("primeNumbers() is called");
        return Arrays.asList(new Object[][] {
           { 2, true },
           { 6, false },
           { 19, true },
           { 22, false },
           { 23, true }
        });
     }

Ater that we create constructor for the test case and initialize the value.

     public TestJunit(Integer inputNumber, 
        Boolean expectedResult) {
       System.out.println("TestJunit-> inputNumber:"+inputNumber+" expectedResult:"+expectedResult);
        this.inputNumber = inputNumber;
        this.expectedResult = expectedResult;
     }

The real testing method is marked with @Test.

     @Test
     public void testPrimeNumberChecker() {
        System.out.println("Parameterized Number is : " + inputNumber);
        assertEquals(expectedResult, logic.validate(inputNumber));
     }

Put everything together.

import static org.junit.Assert.assertEquals;
/*from  ww  w .j  av  a 2  s .  c om*/
import java.util.Arrays;
import java.util.Collection;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class TestJunit {
     private Integer inputNumber;
     private Boolean expectedResult;
     private BusinessLogic logic;

     @Before
     public void initialize() {
        logic = new BusinessLogic();
     }

     public TestJunit(Integer inputNumber, 
        Boolean expectedResult) {
       System.out.println("TestJunit-> inputNumber:"+inputNumber+" expectedResult:"+expectedResult);
        this.inputNumber = inputNumber;
        this.expectedResult = expectedResult;
     }

     @Parameterized.Parameters
     public static Collection primeNumbers() {
       System.out.println("primeNumbers() is called");
        return Arrays.asList(new Object[][] {
           { 2, true },
           { 6, false },
           { 19, true },
           { 22, false },
           { 23, true }
        });
     }

     @Test
     public void testPrimeNumberChecker() {
        System.out.println("Parameterized Number is : " + inputNumber);
        assertEquals(expectedResult, logic.validate(inputNumber));
     }

}




Run

Here is the code to run the test case.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
/*from w w w  .j a  v a 2  s  . c o m*/
public class Main {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }
}

The code above generates the following result.

Example 2

Here is another example for Parameterized Test.

public class BusinessLogic {
  public static int add(int a, int b) {
    return a + b;
  }
}

Test cases

import static org.junit.Assert.assertEquals;
/*from w  w w  . j a va 2 s  . c om*/
import java.util.Arrays;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class TestJunit {
  private int numberA;
  private int numberB;
  private int expected;

  // parameters pass via this constructor
  public TestJunit(int numberA, int numberB, int expected) {
    this.numberA = numberA;
    this.numberB = numberB;
    this.expected = expected;
  }

  // Declares parameters here
  @Parameters(name = "{index}: add({0}+{1})={2}")
  public static Iterable<Object[]> data1() {
    return Arrays.asList(new Object[][] { { 1, 1, 2 }, { 2, 2, 4 },
        { 8, 2, 10 }, { 4, 5, 9 } });
  }

  @Test
  public void test_add() {
    assertEquals(expected, BusinessLogic.add(numberA, numberB));
  }
}

Code to run

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class Main {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }
}