DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Writing Parameterized Tests with JUnit Rules

Writing Parameterized Tests with JUnit Rules

Jens Schauder user avatar by
Jens Schauder
·
Dec. 21, 12 · Interview
Like (1)
Save
Tweet
Share
16.52K 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

  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Kotlin Is More Fun Than Java And This Is a Big Deal
  • How To Avoid “Schema Drift”
  • Problems of Cloud Cost Management: A Socio-Technical Analysis

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: