In jUnit, you can use both @RunWith and @Parameter annotations to pass parameters into a Unit Test.

1. MathUtils – Parameterized Test

Review a simple add method.

MathUtils.java
package com.mkyong.match.utils;
 
public class MathUtils {
 
	public static int add(int a, int b) {
		return a + b;
	}
 
}

Here’s a jUnit test for above class, read the comments for self-explanatory.

MathUtilsTest.java
package com.mkyong.match.utils;
 
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
 
@RunWith(value = Parameterized.class)
public class MathUtilsTest {
 
	private int numberA;
	private int numberB;
	private int expected;
 
	//parameters pass via this constructor
	public MathUtilsTest(int numberA, int numberB, int expected) {
		this.numberA = numberA;
		this.numberB = numberB;
		this.expected = expected;
	}
 
	//Declares parameters here
	@Parameters(name = "{index}: add({0}+{1})={2}")
	public static Iterable<Object[]> data1() {
		return Arrays.asList(new Object[][] { 
			{ 1, 1, 2 }, 
			{ 2, 2, 4 }, 
			{ 8, 2, 10 }, 
			{ 4, 5, 9 } 
		});
	}
 
	@Test
	public void test_add() {	
		assertEquals(expected,MathUtils.add(numberA, numberB));
	}
 
}

What is {0}, {1} and {2}?
If parameter is “{ 3, 4, 7 }”, then {0} = 3, {1} = 4, {2} = 7. Refer to below output :

Output

junit-parameter-example-1

Figure : Eclipse jUnit Plugin Console

2. DomainUtils – Parameterized Test

Review a Domain name validator.

DomainUtils.java
package com.mkyong.regex.utils;
 
import java.util.regex.Pattern;
 
public class DomainUtils {
 
	private static Pattern pDomainName;
 
	private static final String DOMAIN_NAME_PATTERN = "^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$";
 
	static {
		pDomainName = Pattern.compile(DOMAIN_NAME_PATTERN);
	}
 
	//is this a valid domain name?
	public static boolean isValidDomainName(String domainName) {
		return pDomainName.matcher(domainName).find();
	}
 
}

Here’s a jUnit test for above class.

DomainUtilsTest.java
package com.mkyong.regex.utils;
 
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
 
@RunWith(value = Parameterized.class)
public class DomainUtilsTest {
 
	private String domain;
	private boolean expected;
 
	public DomainUtilsTest(String domain, boolean expected) {
		this.domain = domain;
		this.expected = expected;
	}
 
	@Parameters(name= "{index}: isValid({0})={1}")
 	public static Iterable<Object[]> data() {
 		return Arrays.asList(new Object[][] { 
 		{ "google.com", true },
		{ "mkyong.com", true },
                { "-mkyong.com", false },
                { "mkyong-.com", false },
                { "3423kjk", false },
                { "mk#$kdo.com", false }
            }
 	);
     }
 
	@Test
	public void test_validDomains() {
		assertEquals(expected,DomainUtils.isValidDomainName(domain));
	}
 
}

Output

junit-parameter-example-2

Done.

Note
What I don’t like is you have to follow the “jUnit” way to declare the parameter providers, and pass it into the unit test via constructor. If compare with TestNG, TestNG is more flexible in the way of passing the parameters into unit test, read this TestNG parameter test.

References

  1. Domain Name RegEx Example
  2. jUnit Parameterized JavaDoc
  3. TestNG Parameter Test
Tags :
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Related Posts

Popular Posts