About Mike Miller

Mike is a software developer who loves to learn how things work. A Java programmer who caught the Groovy & Grails itch and is always looking for opportunities to include them as part of the solution.

Parameterized JUnit tests

Sometimes you encounter a problem that just screams for using “parameterized” tests rather than copy/pasting the same method many times.   The test method is basically the same and the only thing that changes is the data passed in.  In this case, consider creating a test case that utilitizes the ” Parameterized” class from JUnit.

I recently ran into a problem where our validation of an email address did not allow unicode characters.  The fix was fairly straight-forward, change the regular expression to allow those characters.  Next, it was time to test the change.  Rather than copy/paste separate methods for each set of data, I decided to learn about the Parameterized method.   Below is the result.  The data includes the expected result and the email address to be validated.

JUnit test class

package com.mycompany.client;
 
import static org.junit.Assert.*;
 
import java.util.Arrays;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
 
import com.mycompany.test.TestServiceUtil;
 
/**
 * Parameterized test case for validating email addresses against a regular expression.
 * We need to allow unicode characters in the userid portion of the email address, so 
 * these test cases where created to help validate the validateEmailAddress method
 * in the FieldValidationController class.
 * 
 * @author mmiller
 *
 */
@RunWith(Parameterized.class)
public class TestFieldValiationController {
 
    @Parameters(name = "{index}: {1} is valid email address = {0}")
    public static Iterable<Object> data() {
        return Arrays.asList(new Object[][] { 
         { true, "john@mycomp.com" },           { true,  "john123@mycomp.com" },
         { true, "j+._%20_-brown@mycomp.com" }, { true,  "123@mycomp.com" },
         { false, "john brown@mycomp.com" },    { false, "123@mycomp" },
         { false, "john^brown@mycomp.com" },    { true , "1john@mycomp.com" },
         { false, "john#brown@mycomp.com" },    { false, "john!brown@mycomp.com" },
         { false, "john()brown@mycomp.com" },   { false, "john=brown@mycomp.com" },
         { true,  "johñ.brown@mycomp.com" },    { false, "john.brown@mycomp.coñ" },
         { true,  "johú@mycomp.com" },          { true,  "johíáó@mycomp.com" }
        });
    }
   
    private boolean expected;
    private String emailAddress;
 
    public TestFieldValiationController(boolean expected, String emailAddress) {
        this.expected = expected;
        this.emailAddress = emailAddress;
        TestServiceUtil.getInstance();
    }
 
    @Test
    public void validateEmail() {
        assertEquals(expected, FieldValidationController.getInstance().validateEmailAddress(emailAddress));
    }
}

Hope this helps!
 

Reference: Parameterized JUnit tests from our JCG partner Mike Miller at the Scratching my programming itch 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.

3 Responses to "Parameterized JUnit tests"

  1. ks says:

    I assume the line 46 is redundant?

  2. Christian says:

    Parametrization of tests is easier with TestNG.

  3. Artur Szeja says:

    You didn’t mention that the functionality of customized test names is available since JUnit 4.11

Leave a Reply


eight − 2 =



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