ankush pawar
ankush pawar

Reputation: 61

@DataProvider in testng at class level

I try to run @Test method, but define @Test at class level with dataProvider attribute and also mention method as a @Test, please take look on below code for more idea

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@Test(dataProvider="Data")
public class TestAtClassLevel{

    @Test
    public void testCase(String value) {
        System.out.println("Test case");
    }

    @DataProvider(name="Data")
    public Object[][] dataProvider(){
        String value[][]=new String[2][1];
        value[0][0]="FirstValue";
        value[1][0]="SecondValue";
        return value;
    }
}

I try to run the above code but I get the below exception:

[Utils] [ERROR] [Error] org.testng.TestNGException: 
Cannot inject @Test annotated Method [testCase] with [class java.lang.String].

I am able to run the above code after remove @Test from method-level

Why am I getting an exception even if I declare data provider name at class level?

Upvotes: 2

Views: 891

Answers (1)

Gautham M
Gautham M

Reputation: 4935

If you wanted the testCase method to get data from the data provider then mention that against the test method itself instead of using it at the class level. Or remove the @Test against the method without removing the annotation at class level, so that the method level annotation do not override the class level configuration.

@Test(dataProvider="Data")
public void testCase(String value) {
    System.out.println("Test case");
}

Mentioning dataProviderClass is useful at the class level, when the data providers are defined in a different class and this property would indicate testng to look for data providers in the mentioned class. Still, you need to mention the specific data provider name at the method level itself.

Upvotes: 1

Related Questions