Core Java

JUnit4 Parameterized and Theories Examples

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;

@RunWith(Parameterized.class)

public class ParameterizedTest {

@Parameters

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) {

this.sport = sport;

this.nation = nation;

this.year = year;

this.totWinners = totWinners;

}

@Test

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.

Theories

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 org.hamcrest.CoreMatchers.is;

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;

@RunWith(Theories.class)

public class TheoryTest {

@DataPoint

public static int MARKET_FIRST_GOALSCORERE_ID = 2007;

@DataPoint

public static int MARKET_WDW_ID = 2008;

@DataPoint

public static BigDecimal PRICE_BD = new BigDecimal(6664.0);

@DataPoint

public static double PRICE_1 = 0.01;

@DataPoint

public static double PRICE_2 = 100.0;

@DataPoint

public static double PRICE_3 = 13999.99;

@Theory

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));

}

@Theory

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));

}

@Theory

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.
 

Reference: JUnit4 Parameterized and Theories from our JCG partner Marco Castigliego at the Remove duplication and fix bad names blog.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button