Reputation: 3159
I have tried to use @DataProvider with TestNG and arquillian, but I can't figure it out why it does not work when I use a class that I have created.
If I use it with String, or any primitive datatypes my @Test method successfully receives the DataProvider populated objects.
@DataProvider(name="test")
public Object[][] createdata1() {
return new Object[] { {"test1"}, {"test2"}, {"test2"} };
}
the above method works, but
@DataProvider(name="test")
public Object[][] createdata1() {
return new Object[] { {new User("test1")}, {new User("test2")}, {new User("test2")}};
}
does not. This second method gives me null pointers only.
Any ideas?
Upvotes: 1
Views: 6351
Reputation: 1
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataPro {
@Test(dataProvider = "Sender")
public void Receiver(String first, String second)
{
System.out.print(first);
System.out.print(second);
}
@DataProvider
public Object[][] Sender()
{
Object[][] data = new Object[2][2];
data[0][0] = "a";
data[0][1] = "b";
data[1][0] = "c";
data[1][1] = "d";
return data;
}
}
Upvotes: 0
Reputation: 5319
You have to return an array of arrays, this syntax is motivated by having n-arguments for n-test cases. So the proper syntax would be for returning a User
per test.
package testng;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class SomeTest {
@DataProvider(name = "test")
public Object[][] createdata() {
return new Object[][] {
new Object[] { new User("test1") },
new Object[] { new User("test2") },
new Object[] { new User("test2") } };
}
@Test(dataProvider = "test")
public void xxx_happyPath_success(User user) {
Assert.assertNotNull(user);
}
}
Upvotes: 2
Reputation: 296
Return the following instead.
return new Object[][]{
{new User("test1")},
{new User("test2")},
{new User("test2")}};
Better still, If you are using DataProvider often, you can create the following helper method in helper class to help create data easily
public static Object[][] provideData(Object... arObj) {
Object[][] arObject = new Object[arObj.length][];
int index = 0;
for (Object obj : arObj) {
arObject[index++] = new Object[]{obj};
}
return arObject;
}
Hence the following is more easier to decipher ->
@DataProvider(name="test")
public Object[][] createdata1() {
return provideData(new User("Test1"), new User("Test2"), new User("Test3"));
Upvotes: 7