Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

TestNG @Test Annotation and DataProviderClass Example

DZone's Guide to

TestNG @Test Annotation and DataProviderClass Example

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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

?
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

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

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}