In JUnit, you can pass the parameters into the unit test method via the following methods :

  1. Constructor
  2. Fields injection via @Parameter

P.S Tested with JUnit 4.12

1. MatchUtils – Test with multiple parameters

A simple add operation.

MathUtils.java

package com.mkyong.examples;

public class MathUtils {

    public static int add(int a, int b) {
        return a + b;
    }

}

1.2 MatchUtils – Parameterized via Constructor

The parameters are passed into the test method via constructor.

ParameterizedTest.java

package com.mkyong;

import com.mkyong.examples.MathUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(value = Parameterized.class)
public class ParameterizedTest {

    private int numberA;
    private int numberB;
    private int expected;

    // Inject via constructor
    // for {8, 2, 10}, numberA = 8, numberB = 2, expected = 10
    public ParameterizedTest(int numberA, int numberB, int expected) {
        this.numberA = numberA;
        this.numberB = numberB;
        this.expected = expected;
    }

	// name attribute is optional, provide an unique name for test
	// multiple parameters, uses Collection<Object[]>
    @Parameters(name = "{index}: testAdd({0}+{1}) = {2}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {1, 1, 2},
                {2, 2, 4},
                {8, 2, 10},
                {4, 5, 9},
                {5, 5, 10}
        });
    }

    @Test
    public void test_addTwoNumbes() {
        assertThat(MathUtils.add(numberA, numberB), is(expected));
    }

}

1.3 MatchUtils – Parameterized via Field Injection

The parameters are passed into the test method via field injection.

ParameterizedTest.java

package com.mkyong;

import com.mkyong.examples.MathUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.Parameter;

import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(value = Parameterized.class)
public class ParameterizedTest {

    //default value = 0
    @Parameter(value = 0)
    public int numberA;

    @Parameter(value = 1)
    public int numberB;

    @Parameter(value = 2)
    public int expected;

    @Parameters(name = "{index}: testAdd({0}+{1}) = {2}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {1, 1, 2},
                {2, 2, 4},
                {8, 2, 10},
                {4, 5, 9},
                {5, 5, 10}
        });
    }

    @Test
    public void test_addTwoNumbes() {
        assertThat(MathUtils.add(numberA, numberB), is(expected));
    }

}
Note
For @Parameters, the “name” attribute is optional, it helps you to identify individual test cases by providing a unique name.

What is {0}, {1} and {2}?
If the parameter is “{ 3, 4, 7 }”, then {0} = 3, {1} = 4, {2} = 7.

Output

junit-parameterized-1

2. DomainUtils – Test with single parameter

Another simple class, validate a domain name.

DomainUtils.java

package com.mkyong.examples;

import java.util.regex.Pattern;

public class DomainUtils {

    private static final String DOMAIN_NAME_PATTERN = "^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$";
    private static Pattern pDomainName = Pattern.compile(DOMAIN_NAME_PATTERN);

    public static boolean isValid(String domainName) {
        return pDomainName.matcher(domainName).find();
    }

}

2.1 DomainUtils Parameterized Test

The parameters are passed into the test method via field injection.

ParameterizedTest.java

package com.mkyong;

import com.mkyong.examples.DomainUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(value = Parameterized.class)
public class Parameterized2Test {

    //default value = 0
    @Parameter
    public String domainName;

	//Single parameter, use Object[]
    @Parameters(name = "{index}: testDomain - {0}")
    public static Object[] data() {
        return new Object[]{
                "google.com",
                "mkyong.com",
                "twitter.com"
        };
    }

    @Test
    public void test_valid_domain() {
        assertThat(DomainUtils.isValid(domainName), is(true));
    }

}

Output

junit-parameterized-2
Note
TestNG is more flexible in the way of passing the parameters into unit tests, read this TestNG parameter test.

References

  1. Domain Name RegEx Example
  2. JUnit Parameterized JavaDoc
  3. TestNG Parameter Test
  4. Zohhak - JUnit parameterized made simple
  5. JUnit Wiki - Parameterized