0
0
JUnittesting~10 mins

@CsvFileSource for external CSV in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load CSV data from an external file using @CsvFileSource.

JUnit
@ParameterizedTest
@CsvFileSource(resources = [1])
void testWithCsvFileSource(String input, int expected) {
    assertNotNull(input);
    assertTrue(expected >= 0);
}
Drag options to blanks, or click blank then click option'
A"data/test-data.csv"
B"/data/test-data.csv"
C"/test-data.csv"
D"test-data.csv"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the leading slash in the resource path.
Using an incorrect file path that does not exist.
2fill in blank
medium

Complete the code to skip the first line (header) of the CSV file in @CsvFileSource.

JUnit
@ParameterizedTest
@CsvFileSource(resources = "/data/test-data.csv", [1] = 1)
void testSkipHeader(String input, int expected) {
    assertNotNull(input);
    assertTrue(expected >= 0);
}
Drag options to blanks, or click blank then click option'
AnumLinesToSkip
BskipLines
CskipHeader
DlinesToSkip
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect attribute names like skipLines or skipHeader.
Not skipping the header line causing test failures.
3fill in blank
hard

Fix the error in the @CsvFileSource annotation to correctly specify the file path.

JUnit
@ParameterizedTest
@CsvFileSource(resources = [1])
void testFilePath(String input) {
    assertNotNull(input);
}
Drag options to blanks, or click blank then click option'
A"/data/test.csv"
B"data/test.csv"
C"./data/test.csv"
D"data\\test.csv"
Attempts:
3 left
💡 Hint
Common Mistakes
Using relative paths without leading slash.
Using backslashes which are invalid in resource paths.
4fill in blank
hard

Fill both blanks to correctly configure @CsvFileSource to skip the header and use a UTF-8 encoding.

JUnit
@ParameterizedTest
@CsvFileSource(resources = "/data/test-data.csv", [1] = 1, [2] = "UTF-8")
void testWithEncoding(String input, int expected) {
    assertNotNull(input);
    assertTrue(expected >= 0);
}
Drag options to blanks, or click blank then click option'
AnumLinesToSkip
Bencoding
Ccharset
DskipLines
Attempts:
3 left
💡 Hint
Common Mistakes
Using charset instead of encoding.
Using skipLines which is not a valid attribute.
5fill in blank
hard

Fill all three blanks to create a @CsvFileSource that loads a CSV file, skips the first line, and uses UTF-8 encoding.

JUnit
@ParameterizedTest
@CsvFileSource(resources = [1], [2] = 1, [3] = "UTF-8")
void testFullConfig(String input, int expected) {
    assertNotNull(input);
    assertTrue(expected >= 0);
}
Drag options to blanks, or click blank then click option'
A"/data/test.csv"
BnumLinesToSkip
Cencoding
D"data/test.csv"
Attempts:
3 left
💡 Hint
Common Mistakes
Using relative path without slash.
Wrong attribute names for skipping lines or encoding.