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.

Example:

DivisionTestCase.java
import java.util.Arrays;
import java.util.Collection;
import com.javawithease.business.*;
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 javawithease
*/

@RunWith(Parameterized.class)
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.
@Before
public void initializeDivisionTest

No comments: