Commit 26c311d7 authored by dbhuller's avatar dbhuller

added parameterized test class to test multiple inputs

parent 0e4575ac
package com.calculator;
import java.util.Collection;
public class Calculator {
public int add(int a, int b) {
......
......@@ -4,7 +4,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorTest.class, SampleTestClass.class, TestResultClass.class})
@Suite.SuiteClasses(TestSuite.class)
public class AnnotatedTestRunner {
}
package com.calculator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class ParameterizedCalculatorTest {
Calculator paramCalc;
int firstVal;
int secondVal;
int expectedSum;
@Before
public void init() {
paramCalc = new Calculator();
}
public ParameterizedCalculatorTest(int firstVal, int secondVal, int expectedSum) {
super();
this.firstVal = firstVal;
this.secondVal = secondVal;
this.expectedSum = expectedSum;
}
@Parameterized.Parameters
public static Collection sumValues() {
return Arrays.asList(new Object[][] {
{2, 2, 4},
{4, 4, 8},
{3, 4, 7},
{10, 10, 20}
});
}
@Test
public void testSumValues() {
System.out.println("Sum of " + firstVal + " + " + secondVal + ": " + expectedSum);
assertEquals(expectedSum, paramCalc.add(firstVal, secondVal));
}
}
......@@ -6,7 +6,7 @@ import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(SampleTestClass.class);
Result result = JUnitCore.runClasses(TestSuite.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
......
package com.calculator;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorTest.class, TestResultClass.class, SampleTestClass.class,
ParameterizedCalculatorTest.class})
public class TestSuite {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment