White Box Testing
Test your code
   Home      JUnit4 Data Driven Testing with Theories Class
 
JUnit4 Data Driven Testing with Theories Class
 
Parameterized tests supply static and deterministic set of inputs to a test case, while Theories  tests can supply in-deterministic, infinite number of test data with all combinations of several variables

Parameterized annotation identifies @Test, Theories identifies @Theory to run the test cases

JUnit have the following Annotations to support Theories

@Theory - Identifies the Theory test to run.
@DataPoint - Identifies a single set of test data
@DataPoints - Identifies multiple sets of test data
@ParametersSuppliedBy - Provides parameters to the test cases.
 
ParameterSuppliar.java abstract class that gives parameters that we can supply to the test case.
 
To run your test with Theories execute test with Theories.class runner
 
//Sample code with DataPoint
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
 
@RunWith(Theories.class)
public class TheoryTest1 {
 
                @DataPoint
                public static String a = "a";
 
                @DataPoint
                public static String b = "b";
 
                @Theory
                public void test(String str1, String str2) {
                                System.out.println(str1 + " and " + str2);
                }
}

console output
##########
a and a
a and b
b and a
b and b
 
 
 
//Sample code with DataPoints
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
 
@RunWith(Theories.class)
public class TheoryTest2 {
                @DataPoints
                public static String[] smallChar = { "a", "b" };
 
                @DataPoints
                public static String[] capChar = { "A", "B" };
 
                @DataPoints
                public static String[] num = { "1", "2" };
 
                @Theory
                public void test(String str1, String str2) {
                                System.out.println(str1 + " and " + str2);
                }
}
 
console output
##########
a and a
a and b
a and A
a and B
a and 1
a and 2
b and a
b and b
b and A
b and B
b and 1
b and 2
A and a
A and b
A and A
A and B
A and 1
A and 2
B and a
B and b
B and A
B and B
B and 1
B and 2
1 and a
1 and b
1 and A
1 and B
1 and 1
1 and 2
2 and a
2 and b
2 and A
2 and B
2 and 1
2 and 2
 
 
 
//Sample code with @ParametersSuppliedBy
import java.util.ArrayList;
import java.util.List;
 
import org.junit.experimental.theories.ParameterSignature;
import org.junit.experimental.theories.ParameterSupplier;
import org.junit.experimental.theories.ParametersSuppliedBy;
import org.junit.experimental.theories.PotentialAssignment;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
 
@RunWith(Theories.class)
public class TheoryTest3 {
 
    public static class FirstParameterSuppliar extends ParameterSupplier {
        @Override
        public List getValueSources(ParameterSignature sig) {
            System.out.println("First Parameter Iteration");
            List list = new ArrayList();
            list.add(PotentialAssignment.forValue("firstParam", "1"));
            list.add(PotentialAssignment.forValue("firstParam", "2"));
            return list;
        }
    }
 
    public static class SecondParametersSuppliar extends ParameterSupplier {
        @Override
        public List getValueSources(ParameterSignature sig) {
            System.out.println("Second Parameter Iteration");
            List list = new ArrayList();
           list.add(PotentialAssignment.forValue("secondParam", new String[]{"A" , "B" , "C"}));
            list.add(PotentialAssignment.forValue("secondParam", new String[]{"D"}));
            return list;
        }
    }
 
    @Theory
    public void testGetItems(
        @ParametersSuppliedBy(FirstParameterSuppliar.class)String firstParam ,
        @ParametersSuppliedBy(SecondParametersSuppliar.class)String[] secondParams ) {
      for(String secondParam : secondParams)
            System.out.println(firstParam + " and " + secondParam);
    }
}
 
console output
##########
First Parameter Iteration
Second Parameter Iteration
1 and A
1 and B
1 and C
1 and D
Second Parameter Iteration
2 and A
2 and B
2 and C
2 and D
 
The same above test code can be written as below
 
//Sample code with @ParametersSuppliedBy
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
 
import org.junit.experimental.theories.ParameterSignature;
import org.junit.experimental.theories.ParameterSupplier;
import org.junit.experimental.theories.ParametersSuppliedBy;
import org.junit.experimental.theories.PotentialAssignment;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
 
@RunWith(Theories.class)
public class TheoryTest4 {
 
      @Retention(RetentionPolicy.RUNTIME)
      @ParametersSuppliedBy(FirstParameterSuppliar.class)
      public @interface FirstParameter {
      }
 
      @Retention(RetentionPolicy.RUNTIME)
      @ParametersSuppliedBy(SecondParametersSuppliar.class)
      public @interface SecondParameters {
      }
 
      public static class FirstParameterSuppliar extends ParameterSupplier {
            @Override
            public List getValueSources(ParameterSignature sig) {
                  System.out.println("First Parameter Iteration");
                  List list = new ArrayList();
                  list.add(PotentialAssignment.forValue("firstParam", "1"));
                  list.add(PotentialAssignment.forValue("firstParam", "2"));
                  return list;
            }
      }
 
      public static class SecondParametersSuppliar extends ParameterSupplier {
            @Override
            public List getValueSources(ParameterSignature sig) {
                  System.out.println("Second Parameter Iteration");
                  List list = new ArrayList();
                  list.add(PotentialAssignment.forValue("secondParam", new String[] {
                              "A", "B", "C" }));
                  list.add(PotentialAssignment.forValue("secondParam",
                              new String[] { "D" }));
                  return list;
            }
      }
 
      @Theory
      public void testGetItems(@FirstParameter String firstParam,
                  @SecondParameters String[] secondParams) {
            for (String secondParam : secondParams)
                  System.out.println(firstParam + " and " + secondParam);
      }
}
 

 
External References