I need to create a list of test cases. Each test case contains values of different types: String and Float. Test cases don't necessarily have the same length.
Right now. I'm storing all the test cases in an ArrayList<ArrayList<String>>
. I populate the test cases like this:
ArrayList<ArrayList<String>> testCases = new ArrayList<>();
ArrayList<String> test1 = new ArrayList<>(Arrays.asList("1", "R1.1, R1.2, R2.1", "S", "2", "4"));
testCases.add(test1);
// the same for other test cases
Is there a more efficient way to populate 10 or so test cases. For example:
ArrayList<ArrayList<String>> testCases = new ArrayList<>(
new ArrayList<>(Arrays.asList("1", "R1.1, R1.2, R2.1", "S", "2", "4")),
new ArrayList<>(Arrays.asList("2", "R1.1, R1.2, R2.1", "T", "5", "6", "7", "8"))
)
CodePudding user response:
To handle that issue, I will use Parameterized Tests. Why ? You have so much data to test so one approach could be to create enum representing values of each list based on your own criterias then use @EnumSource of JUnit to inject these values. This link will help you to do that: https://www.baeldung.com/parameterized-tests-junit-5
This example from Baeldung tests all the values from enum Month
@ParameterizedTest
@EnumSource(Month.class) // passing all 12 months
void getValueForAMonth_IsAlwaysBetweenOneAndTwelve(Month month) {
int monthNumber = month.getValue();
assertTrue(monthNumber >= 1 && monthNumber <= 12);
}
CodePudding user response:
Better to avoid ArrayList
and use the easier List.of
to form the set of sub-lists:
List<List<String>> testCases = List.of(
List.of("1", "R1.1, R1.2, R2.1", "S", "2", "4"),
List.of("2", "R1.1, R1.2, R2.1", "T", "5", "6", "7", "8")
);
Note that the above lists are immutable.