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

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

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

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

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