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

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Testcontainers With Kotlin and Spring Data R2DBC
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Role of Data Annotation Services in AI-Powered Manufacturing
  • Annotating Data at Scale in Real Time

Trending

  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Accelerating AI Inference With TensorRT
  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.2K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Testcontainers With Kotlin and Spring Data R2DBC
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Role of Data Annotation Services in AI-Powered Manufacturing
  • Annotating Data at Scale in Real Time

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: