0
0
JUnittesting~5 mins

@CsvFileSource for external CSV in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aresources
Bfiles
Cpath
Dlocation
How do you skip the header row in a CSV file when using @CsvFileSource?
ASet <code>skipHeader=true</code>
BRemove the header manually
CSet <code>numLinesToSkip=1</code>
DUse <code>ignoreHeader=true</code>
If the CSV file is not found, what happens when running a test with @CsvFileSource?
ATest passes automatically
BTest is skipped silently
CTest runs with empty data
DTest fails with an error
Which JUnit feature does @CsvFileSource support?
AParameterized tests
BTest suites
CMocking objects
DTest lifecycle callbacks
Where should the CSV file be placed to be accessible by @CsvFileSource?
AInside the <code>src/main/java</code> folder
BInside the <code>src/test/resources</code> folder
CIn the root project folder
DAnywhere on the computer
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.