We show how to develop a regular expression driven by automated tests in JUnit 5. Download setup from and start playing.

git clone
mvn clean test

Regex Testing: Now for JUnit 5

It’s been a year since the article series about

In the meantime, the popularity of JUnit 5 has grown. It’s time to provide sample code.

Indeed, JUnit 5 will make parameterized tests a first-class citizen. It is a new native feature. However, the JUnit 5 User Guide still marks parameterized tests as experimental.

Providing data by @MethodSource

In order to implement the exact same pattern of 1 self-sufficient simple test class, we choose the @MethodSource test data source over more complex alternatives. I recommend to read the JUnit 5 User Guide, Nicolai Parlog’s article or Ali Dehghani’s article on Baeldung, if you want to learn more.

@MethodSource works by reading a Stream of Arguments’ from a static method result. The second annotation we need is @ParameterizedTest, which replaces @Test on a method.

package de.metamorphant.examples;

import static org.junit.jupiter.api.Assertions.assertEquals;


import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class ParameterizedRegexTest {
  private static final String REGEX = "[-+]?\\d+(\\.\\d+)?([eE][-+]?\\d+)?";

  static Stream<Arguments> testCases() {
    return Stream.of(
        Arguments.of("", false, "empty string"), 
        Arguments.of("a", false, "single non-digit"),
        Arguments.of("1", true, "single digit"), 
        Arguments.of("123", true, "integer"),
        Arguments.of("-123", true, "integer, negative sign"),
        Arguments.of("+123", true, "integer, positive sign"), 
        Arguments.of("123.12", true, "float"),
        Arguments.of("123.12e", false, "float with exponent extension but no value"),
        Arguments.of("123.12e12", true, "float with exponent"),
        Arguments.of("123.12E12", true, "float with uppercase exponent"),
        Arguments.of("123.12e12.12", false, "float with non-integer exponent"),
        Arguments.of("123.12e+12", true, "float with exponent, positive sign"),
        Arguments.of("123.12e-12", true, "float with exponent, negative sign")

  @ParameterizedTest(name = "{index} ==> {2}: is {0} well-formed? {1}")
  public void regexTest(String input, boolean isMatchExpected, String description) {
    Boolean matches = input.matches(REGEX);
    assertEquals(isMatchExpected, matches);

That’s all. Pretty simple, isn’t it? It stays ugly Java code, of course.

Build setup

Of course, we still need to set up the proper dependencies.

<project xmlns=""



A few notes:

  • org.junit.jupiter:junit-jupiter-params is the only difference to an ordinary JUnit 5 project. This artifact contains the parameterized test features.
  • Pinning the surefire plugin to a recent version will already be familiar to you as a JUnit 5 user.
  • Selecting Java 8 (or above) is needed to use the Arguments.of(...) static interface methods.

Run and confirm: It’s green.

mvn clean test

Post header background image by WikimediaImages from Pixabay.

Contact us