0
0
JUnittesting~20 mins

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

Choose your learning style9 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.