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

  • Testcontainers With Kotlin and Spring Data R2DBC
  • 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
  • Quality Assurance in AI-Driven Business Evolution

Trending

  • Amazon Quick: AWS's Agentic Workspace, Explained for Engineers
  • Alternative Structured Concurrency
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  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 user avatar
Jagadeesh Motamarri
·
Oct. 02, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
25.5K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • 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
  • Quality Assurance in AI-Driven Business Evolution

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