0
0
JUnittesting~20 mins

@CsvSource for inline CSV data in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CsvSource Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a JUnit test using @CsvSource with multiple parameters
What is the output of the following JUnit test method when run?

Note: The test asserts that the sum of two integers equals the expected result.
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class CalculatorTest {
    @ParameterizedTest
    @CsvSource({"1, 2, 3", "4, 5, 9", "3, 3, 6"})
    void testAdd(int a, int b, int expected) {
        assertEquals(expected, a + b);
    }
}
AThe test fails on the third case because 3 + 3 is not 6.
BThe test fails on the second case because 4 + 5 is not 9.
CAll 3 test cases pass successfully.
DThe test fails on all cases due to a syntax error in @CsvSource.
Attempts:
2 left
💡 Hint
Check the addition results for each input set.
assertion
intermediate
2:00remaining
Identify the failing assertion in a @CsvSource test
Given the following test method, which assertion will fail during execution?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class StringTest {
    @ParameterizedTest
    @CsvSource({"hello, HELLO", "world, WORLD", "java, Java"})
    void testToUpperCase(String input, String expected) {
        assertEquals(expected, input.toUpperCase());
    }
}
AThe assertion for input 'world' fails.
BAll assertions pass successfully.
CThe assertion for input 'hello' fails.
DThe assertion for input 'java' fails.
Attempts:
2 left
💡 Hint
Check how the expected string compares to the actual uppercase conversion.
locator
advanced
2:00remaining
Best practice for specifying string values with commas in @CsvSource
Which option correctly specifies a string containing a comma in a @CsvSource annotation?
A@CsvSource({"'Hello, World'", "Test"})
B@CsvSource({"Hello, World", "Test"})
C@CsvSource({"""Hello, World""", "Test"})
D@CsvSource({"Hello\, World", "Test"})
Attempts:
2 left
💡 Hint
Look for the correct way to escape or quote commas inside CSV strings.
🔧 Debug
advanced
2:00remaining
Identify the cause of a runtime error in a @CsvSource test
Why does the following test method throw an exception during execution?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class DivisionTest {
    @ParameterizedTest
    @CsvSource({"10, 2, 5", "8, 0, 0"})
    void testDivide(int numerator, int denominator, int expected) {
        assertEquals(expected, numerator / denominator);
    }
}
AThe expected value in the second case is incorrect causing assertion failure.
BDivision by zero occurs in the second test case causing ArithmeticException.
CThe @CsvSource syntax is invalid causing a compilation error.
DThe test method parameters do not match the number of CSV values.
Attempts:
2 left
💡 Hint
Consider what happens when denominator is zero in division.
framework
expert
3:00remaining
Understanding parameter conversion in @CsvSource with custom types
Given the following test and supporting code, what will be the result of running the test?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.converter.SimpleArgumentConverter;

class Color {
    String name;
    Color(String name) { this.name = name; }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Color)) return false;
        return name.equals(((Color) o).name);
    }
}

class ColorConverter extends SimpleArgumentConverter {
    @Override
    protected Object convert(Object source, Class<?> targetType) {
        if (source instanceof String && targetType == Color.class) {
            return new Color(((String) source).toLowerCase());
        } else {
            throw new IllegalArgumentException("Conversion failed");
        }
    }
}

public class ColorTest {
    @ParameterizedTest
    @CsvSource({"RED, red", "Blue, blue", "Green, green"})
    void testColorConversion(@ConvertWith(ColorConverter.class) Color input, Color expected) {
        assertEquals(expected, input);
    }
}
AThe test throws IllegalArgumentException due to failed conversion of expected parameter.
BThe test fails because expected parameters are not converted and cause assertion failures.
CAll test cases pass because input strings are converted to lowercase Color objects matching expected.
DThe test fails compilation because @ConvertWith cannot be applied to multiple parameters.
Attempts:
2 left
💡 Hint
Consider how JUnit converts parameters and which parameters have converters applied.