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

  • Testcontainers With Kotlin and Spring Data R2DBC
  • A Practical Pipeline for Identifying Sensitive Columns Before Test Data Masking
  • Good Data, Bad Metric: A Mutation Testing Pattern for Analytics Engineering
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

Trending

  • The Self-Hosting Math Changed While We Were Still Arguing About Sovereignty
  • GenAI Security Gateway: An End-to-End Architecture
  • Spring AI on Local LLMs
  • LangChainGo vs Genkit Go: Where Genkit Shines

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. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. TestNG @Test Annotation and DataProviderClass Example

TestNG @Test Annotation and DataProviderClass Example

By 
Jagadeesh Motamarri
·
Oct. 02, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
25.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

In the previous post, we have seen an example where dataProvider attribute has been used3 to test methods with different sets of  input data for the same test method.

TestNG provides another attribute dataProviderClass in conjunction with dataProvider to fetch the input data for the test methods from an external class.

The actual class that holds input data is set to the dataProviderClass attribute and datProvider by itself holds the method name where the input data is actually fetched.

Here is a quick example to show how to use dataProviderClass and dataProvide attribute

Code

Service Class

?
view source
print?
01.package com.skilledmonster.example;
02./**
03.* Simple calculator service to demonstrate TestNG Framework
04.*
05.* @author Jagadeesh Motamarri
06.* @version 1.0
07.*/
08.public interface CalculatorService {
09.int sum(int a, int b);
10.int multiply(int a, int b);
11.int div(int a, int b);
12.int sub(int a, int b);
13.}

Service Implementation Class

?
view source
print?
01.package com.skilledmonster.example;
02./**
03.* Simple calculator service implementation to demonstrate TestNG Framework
04.*
05.* @author Jagadeesh Motamarri
06.* @version 1.0
07.*/
08.public class SimpleCalculator implements CalculatorService {
09.public int sum(int a, int b) {
10.return a + b;
11.}
12.public int multiply(int a, int b) {
13.return a * b;
14.}
15.public int div(int a, int b) {
16.return a / b;
17.}
18.public int sub(int a, int b) {
19.return a - b;
20.}
21.}

Data Provider Class

?
view source
print?
01.package com.skilledmonster.common;
02.import org.testng.annotations.DataProvider;
03./**
04.* Data Provider class for TestNG test cases
05.*
06.* @author Jagadeesh Motamarri
07.* @version 1.0
08.*/
09.public class TestNGDataProvider {
10./**
11.* Data Provider for testing sum of 2 numbers
12.*
13.* @return
14.*/
15.@DataProvider
16.public static Object[][] testSumInput() {
17.return new Object[][] { { 5, 5 }, { 10, 10 }, { 20, 20 } };
18.}
19./**
20.* Data Provider for testing multiplication of 2 numbers
21.*
22.* @return
23.*/
24.@DataProvider
25.public static Object[][] testMultipleInput() {
26.return new Object[][] { { 5, 5 }, { 10, 10 }, { 20, 20 } };
27.}
28.}

Finally, test class that uses dataProviderClass attribute to feed the input data for the test methods

?
package com.skilledmonster.example;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.skilledmonster.common.TestNGDataProvider;
/**
* Example to demonstrate use of dataProviderClass and dataProvide attributes of TestNG framework
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class TestNGAnnotationTestDataProviderExample {
public CalculatorService service;
@BeforeClass
public void init() {
System.out.println("@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.");
System.out.println("init service");
service = new SimpleCalculator();
}
@Test(dataProviderClass = TestNGDataProvider.class, dataProvider = "testSumInput")
public void testSum(int a, int b) {
System.out.println("@Test : testSum()");
int result = service.sum(a, b);
Assert.assertEquals(result, a + b);
}
@Test(dataProviderClass = TestNGDataProvider.class, dataProvider = "testMultipleInput")
public void testMultiple(int a, int b) {
System.out.println("@Test : testMultiple()");
int result = service.multiply(a, b);
Assert.assertEquals(result, a * b);
}
}

Output

testng_dataprovider_external_class 

As shown in the above console output, each of the testSum() and testMutiple() methods are invoked with different sets of input data using an external class with dataProviderClass attribute.

Advantage

More flexibility and re-usability of commonly used data across several test classes.

Download

 Download TestNG DataProvider Example
Testing TestNG Data (computing) Annotation

Published at DZone with permission of Jagadeesh Motamarri. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Testcontainers With Kotlin and Spring Data R2DBC
  • A Practical Pipeline for Identifying Sensitive Columns Before Test Data Masking
  • Good Data, Bad Metric: A Mutation Testing Pattern for Analytics Engineering
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

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