TestNG Exception: org.testng.TestNGException: DataProvider should be static
Join the DZone community and get the full member experience.
Join For FreeIssue
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 } }; } }
References: http://testng.org/doc/documentation-main.html#parameters-dataproviders
Published at DZone with permission of Jagadeesh Motamarri, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments