Over a million developers have joined DZone.

Introducing the Regex-tester Library for Java

DZone's Guide to

Introducing the Regex-tester Library for Java

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

regex-tester version 0.1 is an open source project that removes the boiler-plate code needed to test regular expressions with JUnit.

Regular expressions often contain business logic that is important to an application yet are rarely put through rigorous, automated testing. That’s unfortunate because it’s generally so easy to test a regular expression (regex, from here on). In many cases, you just want to know that a given string produces a match when the regex  is applied to it. That’s easy even without regex-tester but tested so infrequently in real software.

Running the regex against a handful of strings in a JUnit test is superior coverage to none at all. That’s easy to do with regex-tester:

public class BasicRegexTest {

   public static List<RegexTestStringInfo> getTestParameters() {
     return Arrays.asList(new RegexTestStringInfo[] {
         new RegexTestStringInfo(true, "com"),
         new RegexTestStringInfo(true, "com.thewonggei"),
         new RegexTestStringInfo(true, "com.thewonggei.regexTester"),
         new RegexTestStringInfo(false, ".com.thewonggei"),
         new RegexTestStringInfo(false, "www.google.com")

   public void test() {}

Running the test suite above will automatically create and execute a test case for each test string specified. Even that is a lot of typing for when you want better coverage of the regex. If you want to test many more strings, you can put them in a simple properties file like this:

#A couple of out-of-range examples first
#These are just the wrong format

#However many test strings you desire

#A whole bunch of matching strings

Then the JUnit test gets even simpler:

@Regex(value="^(19|20)\\d\\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$")
public class DateFormatRegexTest {

   public void test() {}


In both cases, the crucial lines of code declare the important pieces under test: the specialized JUnit test suite to use (@RegexTestSuite), the regex to test with (@Regex) and the strings to execute against the regex (either the @RegexTestStrings method or @RegexTestStringsFile). For each string, a boolean value is supplied that indicates whether or not the regex should produce a match.

Therefore, the meaning of

RegexTestStringInfo(true, "com")

is “the given regex will produce a match against the string ‘com’”. If using the properties file


means “the given regex will not produce a match against the string ‘www.google.com’”.

These two examples show both the library’s current capabilities and all that is required of you to start automating the testing of your regular expressions. There is much yet to do (e.g. I know you already wondered why you can’t test for the number and content of matches in regular expressions with groups defined) that will come out in later versions. For now, I’m anxious to know about any usage of the library and, especially, what you think needs added or fixed. Feel free to open up an issue on GitHub for any usage problems, bugs or feature requests.

Get the Code

The code is under the MIT License and is hosted on GitHub here. See the project’sREADME file for more details.

Get the JARs

Maven coordinates:


Or, download the JARs directly from Maven Central here.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Published at DZone with permission of Nick Watts, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}