In JUnit, you can pass the parameters into the unit test method via the following methods :
- Constructor
- 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
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

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

Note
TestNG is more flexible in the way of passing the parameters into unit tests, read this TestNG parameter test.
TestNG is more flexible in the way of passing the parameters into unit tests, read this TestNG parameter test.
Pingback: free games download()
Pingback: school management system software()
Pingback: free download for windows pc()
Pingback: free download for windows pc()
Pingback: Gil()
Pingback: Mary()
Pingback: gdansk airport car rental()
Pingback: Charming wardrobe()
Pingback: fide.pl()
Pingback: larnaca airport taxis()
Pingback: Recondition Old Batteries()
Pingback: cheap oakleys outlet()
Pingback: songspk()
Pingback: replica oakleys()
Pingback: laundry room organization()
Pingback: https://www.facebook.com/Conjure-Gram-Review-755868687900199/()
Pingback: on line slots()
Pingback: read more()
Pingback: other()
Pingback: pancek durian surabaya()
Pingback: Buy youtube views()
Pingback: read more()
Pingback: https://www.facebook.com/Clickmsg-Review-104600156694597/()
Pingback: songspk()
Pingback: http://bestwebhostinginc.com/()
Pingback: Click MSG Review()
Pingback: strisce sbiancanti()
Pingback: send voice sms()
Pingback: air canada seat sale()
Pingback: https://www.facebook.com/Connect-Explore-Review-612225548967691/()
Pingback: transfer from paris airport to disneyland()
Pingback: songs.pk()
Pingback: songspk()
Pingback: songs pk()
Pingback: http://michiganseo.skyrock.com/()
Pingback: https://optimizationmichigan.wordpress.com/()
Pingback: Manchester Australia()
Pingback: sudraba nams()
Pingback: Henry()
Pingback: sudraba nams()
Pingback: cheap sunglasses()
Pingback: buy Australia degree()
Pingback: Juan()
Pingback: sudraba nams()
Pingback: protein()
Pingback: calcium cloride()
Pingback: D-CHIRO-INOSITOL()