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

TestNG Exception: org.testng.TestNGException: DataProvider should be static

DZone's Guide to

TestNG Exception: org.testng.TestNGException: DataProvider should be static

· 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.

Issue

If you see below exception while running your TestNG test case, you probably forgot to set the methods in your Data Provider Class to static.

Source code of the data provider that caused the above exception -

package com.skilledmonster.common;
import org.testng.annotations.DataProvider;
/**
* Data Provider class for TestNG test cases
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class TestNGDataProvider {
/**
* Data Provider for testing sum of 2 numbers
*
* @return
*/
@DataProvider
public Object[][] testSumInput() {
return new Object[][] { { 5, 5 }, { 10, 10 }, { 20, 20 } };
}
/**
* Data Provider for testing multiplication of 2 numbers
*
* @return
*/
@DataProvider
public Object[][] testMultipleInput() {
return new Object[][] { { 5, 5 }, { 10, 10 }, { 20, 20 } };
}
}
?

Solution

If you want to put your data provider in a different class, it needs to be a static method and you specify the class where it can be found in the dataProviderClass attribute.

Source code that fixed the above exception

?
package com.skilledmonster.common;
import org.testng.annotations.DataProvider;
/**
* Data Provider class for TestNG test cases
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class TestNGDataProvider {
/**
* Data Provider for testing sum of 2 numbers
*
* @return
*/
@DataProvider
public static Object[][] testSumInput() {
return new Object[][] { { 5, 5 }, { 10, 10 }, { 20, 20 } };
}
/**
* Data Provider for testing multiplication of 2 numbers
*
* @return
*/
@DataProvider
public static Object[][] testMultipleInput() {
return new Object[][] { { 5, 5 }, { 10, 10 }, { 20, 20 } };
}
}

Referenceshttp://testng.org/doc/documentation-main.html#parameters-dataproviders

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 }}