Thursday, 8 November 2012

JUnit4 Parameterized and Theories


I always relied on TestNG to pass parameters to test methods in order to give a bit of flexibility to my tests or suites.
However, the same flexibility can be achieved using JUnit4.
To use it it’s simple:

package com.marco.test;
import java.util.Arrays;
import java.util.Collection;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
public class ParameterizedTest {
public static Collection data() {
return Arrays.asList(new Object[][] {
/* Sport Nation year totWinners */
{ “basket”, “usa”, 2002, 5, },
{ “soccer”, “argentina”, 2003, 2 },
{ “tennis”, “spain”, 2004, 10 },
{ “chess”, “ireland”, 2005, 0 },
{ “eatingbananas”, “italy”, 2006, 20 }
private final String sport;
private final String nation;
private final int year;
private final int totWinners;
public ParameterizedTest(String sport, String nation, int year, int totWinners) { = sport;
this.nation = nation;
this.year = year;
this.totWinners = totWinners;
public void test() {
Assert.assertTrue(isDataCorrect(sport, nation, year, totWinners));
private boolean isDataCorrect(String sport2, String nation2, int year2, int totWinners2) {
return true;
JUnit will create an instance of the ParameterizedTest class and run the testCombination() method (or any method marked as @Test) for each row defined in the static collection.


This another interesting feature from JUnit4 that I like.  You use Theories in JUnit 4 to test combinations of inputs using the same test method:

package com.marco.test;
import static;
import java.math.BigDecimal;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
public class TheoryTest {
public static int MARKET_FIRST_GOALSCORERE_ID = 2007;
public static int MARKET_WDW_ID = 2008;
public static BigDecimal PRICE_BD = new BigDecimal(6664.0);
public static double PRICE_1 = 0.01;
public static double PRICE_2 = 100.0;
public static double PRICE_3 = 13999.99;
public void lowTaxRateIsNineteenPercent(int market_id, double price) {
Assume.assumeThat(market_id, is(2008));
Assume.assumeThat(price, is(100.0));
// run your test
Assert.assertThat(price, is(100.0));
public void highTaxRateIsNineteenPercent(int market_id, double price) {
Assume.assumeThat(market_id, is(2007));
Assume.assumeThat(price, is(13999.99));
Assert.assertThat(price, is(13999.99));
public void highTaxRateIsNineteenPercent(int market_id, BigDecimal price) {
Assume.assumeThat(market_id, is(2007));
Assert.assertThat(price, is(BigDecimal.valueOf(6664)));
This time you need to mark the test class as @RunWith(Theories.class) and use @DataPoint to define properties that you want to test.
JUnit will call the methods market as @Theory using all the possible combinations based on the DataPoint provided and the type of the variable. PRICE_BD DataPoint will be used only in the last method, the only one accepting BigDecimal in its method parameters.
Only parameters that satisfy the Assume.assumeThat() condition will make through the asser test. The combinations that don’t satisfy the Assume.assumeThat() condition will be ignored silently.

Have fun...

No comments:

Post a Comment