JUnit Tutorial
Introduction to Junit JUnit Set up JUnit Architecture JUnit Annotations JUnit Fixtures Junit Assertions Junit Categories @Test Annotation Parameters Verification of Exceptions Ignoring tests Time out in JUnit tests Parameterizing tests Test Suite TestWatcher TemporaryFolder ExternalResource Theories in JUnit JUnit Test Runners Execution order of JUnit tests Assumptions in JUnit JUnit and Hamcrest Matchers Running JUnit tests in parallel JUnit and Maven Integration JUnit and Gradle Integration Executing Selenium tests using JUnit test framework Method interceptor and usage JUnit in Intellij IDEA JUnit in EclipseParameterized tests in junit
Sometimes we need to follow same steps to test something. But the input is different. In that case, you do not need to create a separate test method for each input. That’s when parameterized tests come into picture. Below example will illustrate how to use parameterized tests. Test method testOddEven will run 5 times each for 5 input numbers.
package junit_tests;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runners.Parameterized;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertTrue;
@RunWith(Parameterized.class)
public class JunitParametersClass {
private Integer number;
private String expectedResult;
public JunitParametersClass(Integer number,
String expectedResult) {
this.number = number;
this.expectedResult = expectedResult;
}
@Parameterized.Parameters
public static Collection Numbers() {
return Arrays.asList(new Object[][] {
{ 1, "odd" },
{ 2, "even" },
{ 3, "odd"},
{ 4, "even" },
{ 5, "odd"}
});
}
@Test
public void testOddEven() {
System.out.println("Number is : " + number);
if (expectedResult.equalsIgnoreCase("odd"))
assertTrue(number %2!=0);
else
assertTrue(number %2==0);
}
}
Web development and Automation testing
solutions delivered!!