Over a million developers have joined DZone.

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

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

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

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

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 best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

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

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

{{ parent.tldr }}

{{ parent.urlSource.name }}