Saturday, 7 July 2018

Junit: Theories: @TestedOn annotation

If you annotate any @Theory method using @TestedOn annotation, then the int parameter with @TestedOn causes it to be supplied with values from the ints array.

Let me explain with an example.




In the above example, I passed [2, 4] as an argument to a, and [1, 3] as an argument to b.

Junit run the test cases with every combination of arguments a and b.

For example, it runs the test case with below combinations.
a : 2, b : 1
a : 2, b : 3
a : 4, b : 1
a : 4, b : 3



Find the below working example.

TestApp.java

package com.sample.test;

import static org.junit.Assert.assertEquals;

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

@RunWith(Theories.class)
public class TestApp {

 @Theory
 public void strReverseTwice(@TestedOn(ints = { 2, 4 }) int a, @TestedOn(ints = { 1, 3 }) int b) {
  //System.out.println("a : " + a + ", b : " + b);
  assertEquals(a + b, b + a);
 }

}


Previous                                                 Next                                                 Home

No comments:

Post a Comment