Issue
I'm trying to create a unit test for a method that takes an arrayList as an input, and returns a different arrayList as an output.
From what I understood about unit testing, I need to make the data then assert that they're correct, but creating an arrayList with sufficient size for the requirements of the method seems complicated.
method to be tested
public static List<String> processList(List<String> input){
//process array
return new ArrayList<String>();//return new list with new/modded data
}
I can do something like
ArrayList<String> inputList = new ArrayList();
inputList.add("data1");
...
inputList.add("data30");
or make some code that generates a fake array
for(int i=0;i<30;i++){
inputList.add("data"+i);//actual code will be slightly more complicated
}
But I would need a few lines for some calculation and variety as the data isn't as simple as I make it appear in this question.
The first solution will take lots of space, the second seems like it will require testing itself, therefore defeating the purpose of a unit test.
I was also thinking of saving the array elements in a text file accessible by the project, and for every input type, I will access the required text file and convert the lines to an array.
Basically, how do I create several arrays of around 10-30 elements each to a unit test a single method that takes a single array input without making a mess?
Solution
If you want your test to assert multiple inputs then you need to provide the expected outcome for every individual test input. For junit this is usually done using parameterized tests.
If you're afraid of bloating your test code with test data then encoding your fixtures into a file can be a good idea, some libraries like json fixtures might help.
Your question also suggests that you would like to somewhat generate the test cases, this is I think close to the concept of Random testing.
Answered By - nomoa