Recall & Review
beginner
What is the purpose of the
@CsvFileSource annotation in JUnit?It is used to provide external CSV files as a source of parameters for parameterized tests, allowing multiple test cases to run with different input data.
Click to reveal answer
beginner
How do you specify the path to an external CSV file using
@CsvFileSource?You use the
resources folder and provide the relative path in the resources attribute, for example: @CsvFileSource(resources = "/data/test-data.csv").Click to reveal answer
intermediate
What happens if the CSV file specified in
@CsvFileSource is missing or the path is incorrect?The test will fail to run and throw an error indicating that the resource could not be found, so it is important to verify the file path and that the file is included in the test resources.
Click to reveal answer
intermediate
Can
@CsvFileSource handle CSV files with headers?Yes, but you need to skip the header row by setting
numLinesToSkip = 1 in the annotation to avoid treating the header as test data.Click to reveal answer
beginner
Write a simple example of a JUnit 5 parameterized test using
@CsvFileSource with an external CSV file.@ParameterizedTest
@CsvFileSource(resources = "/data/input.csv", numLinesToSkip = 1)
void testWithCsvFileSource(String input, int expected) {
assertNotNull(input);
assertTrue(expected >= 0);
}
Click to reveal answer
What attribute do you use to specify the CSV file path in
@CsvFileSource?✗ Incorrect
The correct attribute to specify the CSV file path is
resources.How do you skip the header row in a CSV file when using
@CsvFileSource?✗ Incorrect
You skip the header row by setting
numLinesToSkip=1 in the annotation.If the CSV file is not found, what happens when running a test with
@CsvFileSource?✗ Incorrect
The test fails with an error indicating the resource could not be found.
Which JUnit feature does
@CsvFileSource support?✗ Incorrect
@CsvFileSource is used for parameterized tests to run the same test with different inputs.Where should the CSV file be placed to be accessible by
@CsvFileSource?✗ Incorrect
CSV files should be placed in
src/test/resources so they are available on the test classpath.Explain how to use
@CsvFileSource to run a parameterized test with data from an external CSV file.Think about how JUnit reads CSV files and passes data to test methods.
You got /4 concepts.
Describe common errors when using
@CsvFileSource and how to fix them.Consider file location and data format issues.
You got /4 concepts.