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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}