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.
What attribute do you use to specify the CSV file path in @CsvFileSource?
Aresources
Bfiles
Cpath
Dlocation
✗ 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?
ASet <code>skipHeader=true</code>
BRemove the header manually
CSet <code>numLinesToSkip=1</code>
DUse <code>ignoreHeader=true</code>
✗ 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?
ATest passes automatically
BTest is skipped silently
CTest runs with empty data
DTest fails with an error
✗ Incorrect
The test fails with an error indicating the resource could not be found.
Which JUnit feature does @CsvFileSource support?
AParameterized tests
BTest suites
CMocking objects
DTest lifecycle callbacks
✗ 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?
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
✗ 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.
Practice
(1/5)
1. What is the main purpose of using @CsvFileSource in JUnit tests?
easy
A. To run the same test multiple times with different data from a CSV file
B. To write data into a CSV file during test execution
C. To generate random test data automatically
D. To skip tests based on CSV file content
Solution
Step 1: Understand the role of @CsvFileSource
@CsvFileSource provides external CSV data to parameterized tests, allowing multiple runs with different inputs.
Step 2: Compare options with this role
Only To run the same test multiple times with different data from a CSV file correctly describes running tests multiple times with CSV data. Others describe unrelated actions.
Final Answer:
To run the same test multiple times with different data from a CSV file -> Option A
Quick Check:
@CsvFileSource runs tests with CSV data = B [OK]
Hint: Remember: @CsvFileSource feeds test data, not writes or skips [OK]
Common Mistakes:
Thinking @CsvFileSource writes data instead of reading
Confusing it with random data generation
Assuming it controls test skipping
2. Which of the following is the correct syntax to skip the header line in a CSV file when using @CsvFileSource?
easy
A. @CsvFileSource(resources = "/data.csv", skipLines = 1)
B. @CsvFileSource(resources = "/data.csv", skipHeader = true)
C. @CsvFileSource(resources = "/data.csv", ignoreFirstLine = true)
D. @CsvFileSource(resources = "/data.csv", numLinesToSkip = 1)
Solution
Step 1: Recall the correct attribute for skipping lines
The correct attribute to skip lines in @CsvFileSource is numLinesToSkip.
Step 2: Match options with correct attribute
Only @CsvFileSource(resources = "/data.csv", numLinesToSkip = 1) uses numLinesToSkip = 1 which skips the first line (header).
Final Answer:
@CsvFileSource(resources = "/data.csv", numLinesToSkip = 1) -> Option D
Quick Check:
Skip header line = numLinesToSkip = 1 [OK]
Hint: Use numLinesToSkip to skip headers, not skipHeader or ignoreFirstLine [OK]
Common Mistakes:
Using non-existent attributes like skipHeader or ignoreFirstLine
The CSV file users.csv has a header line. What is the likely problem when running this test?
medium
A. Test will fail because header line is treated as data causing NumberFormatException
B. Test will pass because header is ignored automatically
C. Test will fail due to missing @Test annotation
D. Test will skip all lines and run zero times
Solution
Step 1: Check if header is skipped
No numLinesToSkip is set, so header line is treated as data.
Step 2: Understand impact on parsing
Trying to convert header "age" to int causes NumberFormatException, failing the test.
Final Answer:
Test will fail because header line is treated as data causing NumberFormatException -> Option A
Quick Check:
Header not skipped causes parse error = A [OK]
Hint: Always skip header lines to avoid parsing errors [OK]
Common Mistakes:
Assuming header is skipped automatically
Forgetting to add numLinesToSkip=1
Confusing @ParameterizedTest with @Test
5. You want to test a method that calculates discounts using data from a CSV file with columns: product,price,discountRate. The CSV has a header line. Which is the best way to write the parameterized test method signature and annotation to correctly read and use this data?