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?
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); } }
Check how @CsvFileSource loads files from the resources folder and the effect of numLinesToSkip.
The @CsvFileSource annotation loads the CSV file from the classpath resource path. The numLinesToSkip=1 skips the header line. The test method receives each row's values as parameters and asserts the sum. Since the file is correctly located and formatted, the test passes for all data rows.
Given this parameterized test using @CsvFileSource with string inputs from an external CSV, which assertion correctly verifies that the input string is not empty?
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? } }
Think about how to check that a string is not empty.
assertFalse(input.isEmpty()) checks that the string is not empty. The other options either check for null or empty incorrectly.
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?
Remember that resource paths are relative to the classpath root and start with a slash.
The resource path in @CsvFileSource must start with a slash and be relative to the classpath root. So "/data/test-data.csv" is correct. The full file system path or missing slash are incorrect.
This test fails with java.lang.IllegalArgumentException: CSV file not found. What is the most likely cause?
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 } }
Check the file location and classpath setup.
The error indicates the file cannot be found in the classpath. The most common cause is that the CSV file is not placed in the src/test/resources folder or the path is incorrect.
When using @CsvFileSource with an external CSV file that contains empty lines, what happens during test execution?
Consider how JUnit handles parameterized tests when parameters are missing.
Empty lines in the CSV cause JUnit to receive no parameters for that test invocation, leading to a ParameterResolutionException because the test method expects parameters.