Junit - Part 2
Junit - Part 2
Test Data management is one of the essential things while doing test automation.We will see how parameterization is done in Junit. Both @RunWith and @ Parameter annotations are used to provide parameter values for the test. @Parameters will return a list and take an Object array as argument.
Parameterization using Junit is explained with the below example.
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value=Parameterized.class)
public class Test2
{
private String Firstname,Lastname,Country;
public Test2(String Firstname, String Lastname,String Country)
{
this.Firstname=Firstname;
this.Lastname=Lastname;
this.Country=Country;
}
@Parameters
public static Collection<Object[]> TestData()
{
Object[][] TestData=new Object[][]{ {"sam","samm","Scotland"},{"wayne","rooney","England"},{"robin","van","Holland"}};
return Arrays.asList(TestData);
}
@Test
public void test()
{
//Consider a scenario where you are testing registration functionality of a website, by entering details of multiple users
System.out.println("Parameterized Firstname is:"+Firstname);
System.out.println("Parameterized Lastname is:"+Lastname);
System.out.println("Parameterized country is:"+Country);
//Code to Enter Firstname,Lastname,Country and clicking on Register needs to written.
}
}
Running a Test suite that is a set of tests at a time is done in Junit by using @Suite and @RunWith parameters .
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
Test1.class,
Test2.class
})
public class TestSuite {
}
Comments
Post a Comment