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
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
View Events Video Library
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Readability in the Test: Exploring the JUnitParams
  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java
  • JUnit 5 Tutorial: Nice and Easy [Video]

Trending

  • Development of Custom Web Applications Within SAP Business Technology Platform
  • Leveraging FastAPI for Building Secure and High-Performance Banking APIs
  • How To Use ChatGPT API in Python for Your Real-Time Data
  • Designing Databases for Distributed Systems
  1. DZone
  2. Coding
  3. Java
  4. Alternative to JUnit Parameterized Classes: junit-dataprovider

Alternative to JUnit Parameterized Classes: junit-dataprovider

Ted Vinke user avatar by
Ted Vinke
·
Nov. 13, 13 · Interview
Like (2)
Save
Tweet
Share
38.32K 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

  • Readability in the Test: Exploring the JUnitParams
  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java
  • JUnit 5 Tutorial: Nice and Easy [Video]

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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: