Bird
Raised Fist0
JUnittesting~20 mins

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

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

Practice

(1/5)
1.

What is the main purpose of using @CsvSource in JUnit tests?

easy
A. To run the same test multiple times with different input data
B. To connect to a database and fetch test data
C. To generate random test data automatically
D. To create a graphical user interface for tests

Solution

  1. Step 1: Understand the role of @CsvSource

    @CsvSource allows specifying multiple sets of input data inline for a single test method.
  2. Step 2: Identify the purpose in testing

    This lets the test run repeatedly with each data set, checking behavior for different inputs.
  3. Final Answer:

    To run the same test multiple times with different input data -> Option A
  4. Quick Check:

    @CsvSource runs tests with multiple inputs [OK]
Hint: Remember: @CsvSource repeats tests with different data [OK]
Common Mistakes:
  • Thinking @CsvSource connects to databases
  • Confusing it with random data generation
  • Assuming it creates UI elements
2.

Which of the following is the correct syntax to use @CsvSource for a test method with two parameters int a and int b?

@ParameterizedTest
@CsvSource({"1, 2", "3, 4"})
void testSum(int a, int b) { ... }
easy
A. @CsvSource({"1; 2", "3; 4"})
B. @CsvSource({"1, 2", "3, 4"})
C. @CsvSource({"1 2", "3 4"})
D. @CsvSource({"1|2", "3|4"})

Solution

  1. Step 1: Check the delimiter used in @CsvSource

    The default delimiter for @CsvSource is a comma (,), so values should be separated by commas.
  2. Step 2: Match the syntax with parameters

    Since the method expects two int parameters, each CSV line must have two comma-separated values.
  3. Final Answer:

    @CsvSource({"1, 2", "3, 4"}) -> Option B
  4. Quick Check:

    Comma separates values in @CsvSource [OK]
Hint: Use commas to separate values inside @CsvSource strings [OK]
Common Mistakes:
  • Using semicolons or spaces instead of commas
  • Using wrong delimiters like |
  • Not matching parameter count with CSV values
3.

What will be the output of the following JUnit test when run with @CsvSource?

@ParameterizedTest
@CsvSource({"2, 3, 5", "4, 5, 9", "1, 1, 3"})
void testAdd(int a, int b, int expected) {
    assertEquals(expected, a + b);
}
medium
A. All tests fail
B. All tests pass
C. The first test fails
D. The last test fails

Solution

  1. Step 1: Evaluate each test case

    Test 1: 2 + 3 = 5 matches expected 5 (pass) Test 2: 4 + 5 = 9 matches expected 9 (pass) Test 3: 1 + 1 = 2 does not match expected 3 (fail)
  2. Step 2: Determine overall result

    Since one test fails, the test suite reports failure for that case.
  3. Final Answer:

    The last test fails -> Option D
  4. Quick Check:

    Check sums against expected values [OK]
Hint: Add inputs and compare to expected for each CSV line [OK]
Common Mistakes:
  • Assuming all tests pass without checking expected values
  • Ignoring the last test case
  • Confusing parameter order
4.

Identify the error in this JUnit test using @CsvSource:

@ParameterizedTest
@CsvSource({"apple, 1", "banana"})
void testFruitCount(String fruit, int count) {
    assertTrue(count > 0);
}
medium
A. assertTrue is used incorrectly
B. Method parameters do not match test data types
C. Missing second value in second CSV line causes error
D. @CsvSource cannot have more than one line

Solution

  1. Step 1: Check CSV lines against method parameters

    The method expects two parameters: a String and an int. The first CSV line has two values, but the second line has only one.
  2. Step 2: Understand the impact of missing value

    Missing the second value in the second line causes a runtime error because the test cannot map parameters properly.
  3. Final Answer:

    Missing second value in second CSV line causes error -> Option C
  4. Quick Check:

    Each CSV line must have values matching parameters [OK]
Hint: Count CSV values to match method parameters exactly [OK]
Common Mistakes:
  • Ignoring missing values in CSV lines
  • Assuming assertTrue usage causes error
  • Thinking @CsvSource limits lines to one
5.

You want to test a method isAdult(int age) that returns true if age is 18 or more, false otherwise. Which @CsvSource usage correctly tests ages 17, 18, and 20 with expected results?

hard
A. @CsvSource({"17, false", "18, true", "20, true"})
B. @CsvSource({"17; false", "18; true", "20; true"})
C. @CsvSource({"17 true", "18 true", "20 true"})
D. @CsvSource({"17|false", "18|true", "20|true"})

Solution

  1. Step 1: Use correct delimiter for @CsvSource

    The default delimiter is a comma, so values must be comma-separated.
  2. Step 2: Match test data with method parameters

    Each line has an int age and a boolean expected result, correctly formatted with commas.
  3. Final Answer:

    @CsvSource({"17, false", "18, true", "20, true"}) -> Option A
  4. Quick Check:

    Comma-separated values match parameters and expected results [OK]
Hint: Use commas to separate all values in @CsvSource [OK]
Common Mistakes:
  • Using semicolons or spaces instead of commas
  • Using wrong delimiters like |
  • Not matching parameter count or types
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