Easy learning with example program codes

JUnit parameterized test

The parameterized test is a new feature introduced in JUnit 4. It provides the facility to execute the same test case again and again with different values.

Steps to create a parameterized test:

1. Test class have to be annotated with @RunWith(Parameterized.class) annotation.

2. Create a public static method with @Parameters annotation which returns a collection of objects as test data set.

3. Create data members for each column of test data set.

4. Create a public constructor which takes one object of test data set.

5. Create test case using test data set.


import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runners.Parameterized;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
 * This is test case class for parameterised test case.
 * @author codesjava
public class DivisionTestCase {
	 private int num1;
	 private int num2;
	 private int expectedResult;
	 private DivisionTest divisionTest;
	 //Constructor that takes test data.
	 public DivisionTestCase(int num1, int num2, 
			 int expectedResult){
		 this.num1 = num1;
		 this.num2 = num2;
		 this.expectedResult = expectedResult;
	 //called before every test case.
	 public void initializeDivisionTest() {
		 divisionTest = new DivisionTest();
	 //It will initialise parameters every 
	 //time trigger executes.
     public static Collection primeNumbers() {
        return Arrays.asList(new Object[][] {
           { 10, 2, 5 },
           { 20, 5, 4 },
           { 20, 10, 2 },
           { 40, 10, 20 }
	//Test case for division
	public void test() {
		System.out.println("Parameterized Numbers: num1 = "
	           + num1 + ", num2 = " + num2);
		assertEquals(expectedResult, divisionTest
				.division(num1, num2));

 * This is simple java class containing division method.
 * @author codesjava
public class DivisionTest {	
	//division method
	public int division(int num1, int num2) 
			throws ArithmeticException{
		return num1/num2;


Parameterized Numbers: num1 = 10, num2 = 2
Parameterized Numbers: num1 = 20, num2 = 5
Parameterized Numbers: num1 = 20, num2 = 10
Parameterized Numbers: num1 = 40, num2 = 10



Download this example.
Next Topic: Junit test runner.
Previous Topic: JUnit suite test.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki