TestNG @Test Annotation and DataProviderClass Example
Join the DZone community and get the full member experience.
Join For FreeIn 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
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
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
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
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
Published at DZone with permission of Jagadeesh Motamarri, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments