Kiddo
Kiddo

Reputation: 1990

TestNG - Cannot add a list of String as data provider

I'm trying to pass a list of string as a DataProvider, here is my code:

    @DataProvider(name="test-urls")
    public Object[] testUrls(){
        ArrayList<Object> data = new ArrayList<>();
        // do something here to add elements
        // i.e: data.add("test string 1");
        return new Object[] {data};     
    }

Here is the test:

    @Test(enabled = true, dataProvider = "test-urls")
    public void test(String url) {
        System.out.println("url: " + url);
    }

It looks like the simple test fails. Can you please show me what is wrong with my code? Thank you

Upvotes: 0

Views: 1007

Answers (2)

Serhii Korol
Serhii Korol

Reputation: 861

If you don't like an old-school DP style, it's worth to try a TestNG extension named test-data-supplier, which is an entity-driven DP. Then your sample will become as simple as:

@DataSupplier(name="test-urls")
public List<String> testUrls(){
    return Arrays.asList("url1", "url2", "url3");
}
@Test(enabled = true, dataProvider = "test-urls")
public void test(String url) {
    System.out.println("url: " + url);
}

Upvotes: 0

user1207289
user1207289

Reputation: 3273

You have to return Object[][] or Object[] or Iterator<Object[] as per this

plus, in your case, receive with the same type as ArrayList<string>

@DataProvider(name="test-urls")
    public Object[][] testUrls(){
        ArrayList<string> data = new ArrayList<>();
        // do something here to add elements
        // i.e: data.add("test string 1");
        return new Object[][] {data};     
    }

 @Test(enabled = true, dataProvider = "test-urls")
    public void test( ArrayList<string> list) {
        System.out.println(list);
    }

Upvotes: 0

Related Questions