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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "JUnit4 Parameterized and Theories Examples"

Leave a Reply


five + = 6



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close