Parameterized testing is good to have different data to feed into your test. However, I created a sample calculator that I want to create parameterized tests for. However, I have found that you can only create 1 set of parameterized data for a single test.
I have created parameterized test for adding 2 numbers with the expected result. This data will not work with the subtract as the expected result will be different.
Is it possible to have parameterized data for each test for add, subtract, multiply, and divide?
Many thanks for any suggestions,
@RunWith(Parameterized.class)
public class CalculatorModelPresenterTest {
private CalculatorModel mCalculatorModel;
/* Array of tests */
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{3.0, 4.0, 7.0},
{4.0, 3.0, 7.0},
{8.0, 2.0, 10.0},
{-1.0, 4.0, 3.0},
{3256.0, 4.0, 3260.0}
});
}
private double mNumberOne;
private double mNumberTwo;
private double mExpectedResult;
/* CONSTRUCTOR THAT ASSIGNS THE FIELDS WITH THE TEST DATA */
public CalculatorModelPresenterTest(double numberOne, double numberTwo, double expectedResult) {
mNumberOne = numberOne;
mNumberTwo = numberTwo;
mExpectedResult = expectedResult;
}
/* THIS TEST WILL PASS AS THE TEST DATA IS FOR ADDING */
@Test
public void testAdd() throws Exception {
final double actualResult = mCalculatorModel.add(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
/* HOWEVER, THIS TEST WILL ALWAYS FAIL AS THE TEST DATA IS CUSTOMIZED FOR THE ADD */
@Test
public void testSub() throws Exception {
final double actualResult = mCalculatorModel.sub(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
@Before
public void setUp() throws Exception {
mCalculatorModel = new CalculatorModel();
}
@After
public void tearDown() throws Exception {
mCalculatorModel = null;
}
}
mCalculatorModel
tonull
in thetearDown
method because JUnit creates a new instance of the classCalculatorModelPresenterTest
for each set of parameters. – Gene