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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Introduction to Data-Driven Testing With JUnit 5: A Guide to Efficient and Scalable Testing
  • Why Testing is a Long-Term Investment for Software Engineers
  • TestNG vs. JUnit: A Comparative Analysis of Java Testing Frameworks
  • Mastering Unit Testing and Test-Driven Development in Java

Trending

  • New York City Taxi Trips: Putting Neo4j and DuckDB to Work
  • When a Coding Agent Fixes the Test Instead of the Bug
  • Member Spotlight: Shai Almong
  • Introduction to Error Prone: How to Catch Bugs at Compile Time

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Alternative to JUnit Parameterized Classes: junit-dataprovider

Alternative to JUnit Parameterized Classes: junit-dataprovider

By 
Ted Vinke
·
Nov. 13, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
39.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

we all know junit test-classes can be parameterized , which means that for a given set of test-elements, the test class is instantiated a few times, but using constructors for that isn’t always what you want.

i’ve taken the stringsorttest from this blog as an example.

@runwith(parameterized.class)
public class stringsorttest {

    @parameters
    public static collection<object[]> data() {
        return arrays.aslist(new object[][] {
                         { "abc", "abc" },
                         { "cba", "abc" },
         });
    }

    private final string input;
    private final string expected;

    public stringsorttest(final string input, final string expected) {
        this.input = input;
        this.expected = expected;
    }

    @test
    public void testsort() {
        assertequals(expected, mysortmethod(input));
    }
}
this is pretty darn obnoxious sometimes if you have multiple sets of data for various tests, which all go through the constructor, which would force you to write multiple test classes. testng solves this better by allowing you to provide separate data sets to individual test methods using the @dataprovider annotation .

but don’t worry, now you can achieve the same with the junit-dataprovider , available on github. pull in the dependency with e.g. maven.

<dependency>
       	<groupid>com.tngtech.java</groupid>
       	<artifactid>junit-dataprovider</artifactid>
       	<version>1.5.0</version>
       	<scope>test</scope>
</dependency>
the above example now could be rewritten as:
@runwith(dataproviderrunner.class)
public class stringsorttest {

    @dataprovider
    public static object[][] data() {
        return new object[][] {
                         { "abc", "abc" },
                         { "cba", "abc" },
         };
    }

    @test
    @usedataprovider("data")
    public void testsort(final string input, final string expected) {
       assertequals(expected, mysortmethod(input));
    }
}
you’ll see: no constructor. in this example it doesn’t have many benefits, except maybe for less boiler-plate, but now you can create as many @dataprovider-annotated methods which are fed directly to your @usedataprovider-annotated testmethod(s).

sidenote for eclipse: if you’re using that ide, the junit plugin is unable to map the names of the passed/failed testmethods to the ones in the testclass correctly. if a method fails, you’ll have to find it back manually in the testclass (or vote for the patch andreas smidt created to get this fixed in eclipse).

junit-dataprovider-fails

in the mean time, if you’re stuck with junit and you’d love to use this feature you’re so accustomed to using with testng, go ahead and try the junit-dataprovider now.

JUnit

Published at DZone with permission of Ted Vinke. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Introduction to Data-Driven Testing With JUnit 5: A Guide to Efficient and Scalable Testing
  • Why Testing is a Long-Term Investment for Software Engineers
  • TestNG vs. JUnit: A Comparative Analysis of Java Testing Frameworks
  • Mastering Unit Testing and Test-Driven Development in Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook