White Box Testing
Test your code
   Home      JUnit4 Data Driven Testing with Parameterized Class
 

 
Parameterized unit test allow numbers of test data on same test case. For example you want to test numbers of test data for boundary values and equivalence partition test data
 
Use @RunWith annotations with Parameterized.class, It marks the test class to run with Parameterized.class runner
 
Annotation @Parameter used to provide parameter values for unit test. That method has to be static and should return a collection, and the items you added to this @parameter method collection will be passed to test class constructor
 
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
 
@RunWith(value = Parameterized.class)
public class CalculatorTestAdd {
   private int op1;
   private int op2;
   private int expResult;

public CalculatorTestAdd(int input1, int input2, int expected) {
   op1 = input1;
   op2 = input2;
   expResult = expected;
}
 
@Parameters
public static Collection data() {
   return Arrays.asList(new Object[][] { { 0, 5, 5 }, { 1, 5, 6 }, { 100, 5, 105 }});
}
 
@Test
public final void testAdd() {
   Calculator cal = new Calculator();
   assertEquals("actual not matching with expected", cal.add(op1,op2),expResult);
   }
}