12:46 am - Monday August 21, 2017

Testing Parameterized method with JUNIT

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

PARAMETERIZED TEST

Parameterized tests allow developer to run the same test over and over again different values.

The expected parameter is used along with @Test annotation like @Test(expected)in action.

There are five steps,

a. Annotate test class with @RunWith(Parameterized class).e.g-@runWith(value=Parametrized.class)

b. Create a public static method annotated with @parameters that return a collections of object(as Array) as test data set..e.g- public static Collection<Object[]>data.

c. Create a public constructor that takes in what is equivalent to one “row” of test data.

d. Create an instance variable for each “column” of test data.

e. Instance variables as the source of the test data

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 Junitt {

 private int number;
 public Junitt(int number) {

    this.number = number;

 }

 @Parameters

 public static Collection<Object[]> data() {

   Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
   return Arrays.asList(data);

 }

 @Test

 public void pushTest() {

   System.out.println("Parameterized Number is : " + number);

 }
 }

Output

Parameterized Number is : 1

Parameterized Number is : 2

Parameterized Number is : 3

Parameterized Number is : 4

BACK

Filed in: Z-A All Java Codes

Comments are closed.