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 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

  • LLM Agents and Getting Started with Them
  • Key Takeaways From Integrating a RAG Application With LangSmith
  • A 5-Step SOC Guide That Meets RBI Expectations and Strengthens Security Operations
  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  1. DZone
  2. Coding
  3. Java
  4. Alternative to JUnit Parameterized Classes: junit-dataprovider

Alternative to JUnit Parameterized Classes: junit-dataprovider

By 
Ted Vinke user avatar
Ted Vinke
·
Nov. 13, 13 · Interview
Likes (2)
Comment
Save
Tweet
Share
39.4K Views

Join the DZone community and get the full member experience.

Join For Free

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

The likes didn't load as expected. Please refresh the page and try again.

  • 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