View Ravichandran J.V.'s profile on LinkedIn RJV 's Blog: Parameterized test example - JUnit Tweet @ravichandranjv

Translate

Sunday, 11 November 2012

Parameterized test example - JUnit


import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.junit.BeforeClass;
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 ParameterAnnotation {
private int number;
public ParameterAnnotation(int number) {
   this.number = number;
}
private static int currentIndex, nextIndex;
@BeforeClass
public static void initClassValues(){
currentIndex=0;
nextIndex=0;
}
private static Object[][] data=new Object[5][1];

@Parameters
public static List data() {
       data[0][0]=1;
       data[1][0]=2;
       data[2][0]=3;
       data[3][0]=4;
       data[4][0]=5;
       return Arrays.asList(data);
}
@Test
public void pushTest() {
assertEquals(number, data[currentIndex][nextIndex]);
currentIndex++;
}
}

2 comments:

Tomek Kaczanowski said...

Hello,

I have to admit I do not like the existing parameterized tests mechanism of JUnit. I like much more to use code.google.com/p/junitparams/. Also there is a new initiative which should bring much better support for such tests - see http://tech.groups.yahoo.com/group/junit/message/24084

Jv.Net said...

Thanks, Tomek, for the info will check out the params.

Jv