DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Writing Parameterized Tests with JUnit Rules

Writing Parameterized Tests with JUnit Rules

Jens Schauder user avatar by
Jens Schauder
·
Dec. 21, 12 · Java Zone · Interview
Like (1)
Save
Tweet
16.36K Views

Join the DZone community and get the full member experience.

Join For Free

I can’t help repeating myself: JUnit Rules are among the best, maybe the best, feature of JUnit. I even gave a talk at Devoxx about Junit Rules.

The great thing about Rules compared to other options to solve similar problems like TestRunners or common super classes is that you can combine multiple Rules. One favorite example why you want to do this are Parameterized Tests they are nice, but what if you want to parameterize Tests build on the base of the Spring JUnit Test Runner. It doesn’t work, because you can have only one Test Runner.

The problem with this example is: There aren’t replacements (as far as I know) for the two runners with Rules. The Spring Runner should be replaceable in principle, but it seems to be not as easy as one might think.

But when I was preparing my Devoxx talk it occurred to me that you can actually do Parameterized Tests using Rules! And I think the usage is actually more intuitive then Parameterized tests with its custom annotations and stuff.

It looks like this:

    public class SimpleExampleTest {
     
        @Rule
        public Generator<String> params =
            new ListGenerator(asList("alpha", "beta", "gamma"));
     
        @Test
        public void testSomething() throws Exception {
            assertTrue(params.value().length() >= 4);
        }
    }

You just specify a ListGenerator rule, with all the values you want to run your tests with. Each test in the class gets executed once for each parameter value provided, and it can access the parameter value using the value() method of the rule. I think with the current feature set available for Java and JUnit it doesn’t get much easier to read and write.

The rule itself uses an ErrorCollector to gather test failures so if tests fail you’ll see all the failures not just the last one or something.

The code for Parameterized JUnit Tests with Rules is available at GitHub. Just keep in mind, it is an experiment and demonstration of concept I hacked together at Devoxx.

Testing JUnit

Published at DZone with permission of Jens Schauder, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • AWS, Azure, and GCP: Find the Right Platform
  • How API Management Can Ease Your Enterprise Cloud Migration
  • Tools and Integrations to Significantly Improve Code Review in GitHub
  • Send Push Notifications to Users of Another App

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo