junit Tests Unit testing using Theories


Example

From JavaDoc

The Theories runner allows to test a certain functionality against a subset of an infinite set of data points.

Running theories

import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class FixturesTest {

    @Theory
    public void theory(){
        //...some asserts
    }
}

Methods annotated with @Theory will be read as theories by Theories runner.

@DataPoint annotation

@RunWith(Theories.class)
public class FixturesTest {

    @DataPoint
    public static String dataPoint1 = "str1";
    @DataPoint
    public static String dataPoint2 = "str2";
    @DataPoint
    public static int intDataPoint = 2;

    @Theory
    public void theoryMethod(String dataPoint, int intData){
        //...some asserts
    }
}

Each field annotated with @DataPoint will be used as a method parameter of a given type in the theories. In example above theoryMethod will run two times with following parameters: ["str1", 2] , ["str2", 2]

@DataPoints annotation @RunWith(Theories.class) public class FixturesTest {

    @DataPoints
    public static String[] dataPoints = new String[]{"str1", "str2"};
    @DataPoints
    public static int[] dataPoints = new int[]{1, 2};

    @Theory
    public void theoryMethod(String dataPoint, ){
        //...some asserts
    }
}

Each element of the array annotated with @DataPoints annotation will be used as a method parameter of a given type in the theories. In example above theoryMethod will run four times with following parameters: ["str1", 1], ["str2", 1], ["str1", 2], ["str2", 2]