Aşğaıdaki gibi bir sınıfımız olsun:
public class Toplar {
public int topla(int a, int b){
return a+b;
}
}
Bu sınıfdaki topla methodu için Theory sınıfı aşağıdaki gibidir:
import org.junit.experimental.theories.*;
import org.junit.runner.*;
import static org.junit.Assert.*;
@RunWith(Theories.class)
public class TheoryTest {
@DataPoints public static int[] first = new int[] {1, 2};
@DataPoints public static int[] second = new int[] {3, 4};
@Theory
public void testTopla(int a, int b) {
System.out.println(a+"+"+b);
assertEquals(a+b, (new Toplar()).topla(a, b));
}
}
DataPoints ile kullanılabilecek veriler verilmektedir. Verilen iki sayının + ile toplaı ile toplam fonksiyonudan gelen değerlerin tutması gerekiyor. Test çalıştırıldığında tüm olasılıklar ekrana basılmıştır:
1+1
1+2
1+3
1+4
2+1
2+2
2+3
2+4
3+1
3+2
3+3
3+4
4+1
4+2
4+3
4+4