java - Convert List<T> to Collection<Object[]> for JUnit Parametrized Test -
i perform junit parametrized test data being external. have list of objects , need know how can convert collection of object arrays. see below stack overflow question, want add data file read method.
parameterized junit tests non-primitive parameters?
working code: this:
@runwith(parameterized.class) public class sampletest { private branchmailchildsample branch; public sampletest(branchmailchildsample branch) { this.branch = branch; } @parameters public static collection<object[]> data() { string excel = "c:\\resources\\testdata\\excelsheets\\branchmail\\branchmail_testdata.xlsx"; excelmarshallertool tool = new excelmarshallertool(excel); list<branchmailchildsample> items = tool.unmarshallexcel(branchmailchildsample.class); //right here need help: convert list collection<object[]> //return items collection of object arrays } @test public void test() { system.out.println(branch.tostring()); } }
you don't have convert list.
@runwith(parameterized.class) public class sampletest { @parameters(name = "{0}") public static list<branchmailchildsample> data() { string excel = "c:\\resources\\testdata\\excelsheets\\branchmail\\branchmail_testdata.xlsx"; excelmarshallertool tool = new excelmarshallertool(excel); return tool.unmarshallexcel(branchmailchildsample.class); } @parameter(0) public branchmailchildsample branch; @test public void test() { system.out.println(branch.tostring()); } }
i used field injection, because needs less code constructor injection. setting name object under test prints more helpful output. please have @ documentation of parameterized runner.
you can use constructor injection if don't public field.
@runwith(parameterized.class) public class sampletest { @parameters(name = "{0}") public static list<branchmailchildsample> data() { string excel = "c:\\resources\\testdata\\excelsheets\\branchmail\\branchmail_testdata.xlsx"; excelmarshallertool tool = new excelmarshallertool(excel); return tool.unmarshallexcel(branchmailchildsample.class); } private final branchmailchildsample branch; public sampletest(branchmailchildsample branch) { this.branch = branch; } @test public void test() { system.out.println(branch.tostring()); } }
Comments
Post a Comment