Bird
Raised Fist0
JUnittesting~20 mins

@CsvFileSource for external CSV in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CsvFileSource Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of JUnit test using @CsvFileSource with external CSV

Consider this JUnit 5 test method that uses @CsvFileSource to read data from an external CSV file named data.csv located in src/test/resources. What will be the output when the test runs?

JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {

    @ParameterizedTest
    @CsvFileSource(resources = "/data.csv", numLinesToSkip = 1)
    void testAdd(int a, int b, int expectedSum) {
        assertEquals(expectedSum, a + b);
    }
}
AThe test passes for all rows in data.csv except the header line.
BThe test fails because the CSV file path is incorrect.
CThe test throws a runtime exception due to missing default constructor.
DThe test is ignored because @CsvFileSource requires inline CSV data.
Attempts:
2 left
💡 Hint

Check how @CsvFileSource loads files from the resources folder and the effect of numLinesToSkip.

❓ assertion
intermediate
1:30remaining
Correct assertion for @CsvFileSource test with string inputs

Given this parameterized test using @CsvFileSource with string inputs from an external CSV, which assertion correctly verifies that the input string is not empty?

JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import static org.junit.jupiter.api.Assertions.*;

public class StringTest {

    @ParameterizedTest
    @CsvFileSource(resources = "/strings.csv")
    void testNotEmpty(String input) {
        // Which assertion is correct here?
    }
}
AassertFalse(input.isEmpty());
BassertTrue(input == null);
CassertEquals(input.length(), 0);
DassertNull(input);
Attempts:
2 left
💡 Hint

Think about how to check that a string is not empty.

❓ locator
advanced
1:30remaining
Correct resource path for @CsvFileSource external CSV file

You want to load a CSV file named test-data.csv located in src/test/resources/data/ folder using @CsvFileSource. Which resource path is correct?

A"src/test/resources/data/test-data.csv"
B"data/test-data.csv"
C"/data/test-data.csv"
D"/src/test/resources/data/test-data.csv"
Attempts:
2 left
💡 Hint

Remember that resource paths are relative to the classpath root and start with a slash.

🔧 Debug
advanced
2:00remaining
Identify the cause of failure in @CsvFileSource test

This test fails with java.lang.IllegalArgumentException: CSV file not found. What is the most likely cause?

JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

public class DebugTest {

    @ParameterizedTest
    @CsvFileSource(resources = "/testdata.csv")
    void testData(int x, int y) {
        // test logic
    }
}
AThe test method parameters do not match the CSV columns.
BThe CSV file is not located in the classpath root or resources folder.
CThe CSV file has a header but numLinesToSkip is not set.
DThe test method is missing the @Test annotation.
Attempts:
2 left
💡 Hint

Check the file location and classpath setup.

❓ framework
expert
2:30remaining
Behavior of @CsvFileSource with empty lines in external CSV

When using @CsvFileSource with an external CSV file that contains empty lines, what happens during test execution?

AJUnit fails to start tests and reports a syntax error in the CSV.
BJUnit skips empty lines automatically and runs tests only on valid rows.
CJUnit treats empty lines as rows with empty strings and passes empty parameters.
DJUnit throws a <code>ParameterResolutionException</code> due to missing parameters.
Attempts:
2 left
💡 Hint

Consider how JUnit handles parameterized tests when parameters are missing.

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

  1. Step 1: Understand the role of @CsvFileSource

    @CsvFileSource provides external CSV data to parameterized tests, allowing multiple runs with different inputs.
  2. 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.
  3. Final Answer:

    To run the same test multiple times with different data from a CSV file -> Option A
  4. 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

  1. Step 1: Recall the correct attribute for skipping lines

    The correct attribute to skip lines in @CsvFileSource is numLinesToSkip.
  2. Step 2: Match options with correct attribute

    Only @CsvFileSource(resources = "/data.csv", numLinesToSkip = 1) uses numLinesToSkip = 1 which skips the first line (header).
  3. Final Answer:

    @CsvFileSource(resources = "/data.csv", numLinesToSkip = 1) -> Option D
  4. 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
  • Confusing skipLines with numLinesToSkip
  • Forgetting to skip header causing test failures
3. Given the CSV file data.csv with content:
name,age
Alice,30
Bob,25

and the test method:
@ParameterizedTest
@CsvFileSource(resources = "/data.csv", numLinesToSkip = 1)
void testUser(String name, int age) {
    System.out.println(name + "-" + age);
}

What will be the printed output when the test runs?
medium
A. name-age Alice-30 Bob-25
B. Alice-30 Bob-25
C. Alice-30
D. Test fails due to header line

Solution

  1. Step 1: Understand numLinesToSkip effect

    numLinesToSkip=1 skips the header line "name,age" so only data lines remain.
  2. Step 2: Identify printed lines

    Test runs twice, printing "Alice-30" and "Bob-25" each on separate lines.
  3. Final Answer:

    Alice-30 Bob-25 -> Option B
  4. Quick Check:

    Header skipped, prints data lines only = D [OK]
Hint: Skip header to avoid printing it as data [OK]
Common Mistakes:
  • Not skipping header causing it to print as data
  • Expecting header line to print
  • Confusing output format with CSV file content
4. Consider this test snippet:
@ParameterizedTest
@CsvFileSource(resources = "/users.csv")
void testUser(String name, int age) {
    assertTrue(age > 0);
}

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

  1. Step 1: Check if header is skipped

    No numLinesToSkip is set, so header line is treated as data.
  2. Step 2: Understand impact on parsing

    Trying to convert header "age" to int causes NumberFormatException, failing the test.
  3. Final Answer:

    Test will fail because header line is treated as data causing NumberFormatException -> Option A
  4. 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?
hard
A. @ParameterizedTest @CsvFileSource(resources = "/discounts.csv", numLinesToSkip = 1) void testDiscount(String product, String price, String discountRate) { ... }
B. @ParameterizedTest @CsvFileSource(resources = "/discounts.csv") void testDiscount(String product, int price, int discountRate) { ... }
C. @ParameterizedTest @CsvFileSource(resources = "/discounts.csv", numLinesToSkip = 1) void testDiscount(String product, double price, double discountRate) { ... }
D. @ParameterizedTest @CsvFileSource(resources = "/discounts.csv", skipHeader = true) void testDiscount(String product, double price, double discountRate) { ... }

Solution

  1. Step 1: Identify header skipping attribute

    Use numLinesToSkip = 1 to skip the CSV header line.
  2. Step 2: Match method parameters to CSV columns and types

    Product is String, price and discountRate are decimal numbers, so use double for price and discountRate.
  3. Step 3: Check options for correctness

    @ParameterizedTest @CsvFileSource(resources = "/discounts.csv", numLinesToSkip = 1) void testDiscount(String product, double price, double discountRate) { ... } correctly uses numLinesToSkip=1 and matching parameter types. @ParameterizedTest @CsvFileSource(resources = "/discounts.csv") void testDiscount(String product, int price, int discountRate) { ... } misses skipping header and uses int instead of double. @ParameterizedTest @CsvFileSource(resources = "/discounts.csv", numLinesToSkip = 1) void testDiscount(String product, String price, String discountRate) { ... } uses String for numeric fields. @ParameterizedTest @CsvFileSource(resources = "/discounts.csv", skipHeader = true) void testDiscount(String product, double price, double discountRate) { ... } uses invalid attribute skipHeader.
  4. Final Answer:

    @ParameterizedTest @CsvFileSource(resources = "/discounts.csv", numLinesToSkip = 1) void testDiscount(String product, double price, double discountRate) { ... } -> Option C
  5. Quick Check:

    Skip header with numLinesToSkip=1 and match types = C [OK]
Hint: Use numLinesToSkip=1 and match parameter types to CSV columns [OK]
Common Mistakes:
  • Using skipHeader instead of numLinesToSkip
  • Not skipping header causing parse errors
  • Mismatching parameter types with CSV data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard