0
0
JUnittesting~5 mins

@CsvSource for inline CSV data in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @CsvSource annotation in JUnit?

@CsvSource allows you to provide inline CSV (comma-separated values) data directly in your test method. It helps run the same test multiple times with different inputs easily.

Click to reveal answer
beginner
How do you write multiple test cases using @CsvSource?

List each test case as a string inside @CsvSource, separated by commas. Each string contains comma-separated values representing parameters for one test run.

Example: @CsvSource({"1, 2, 3", "4, 5, 9"}) runs two tests with parameters (1,2,3) and (4,5,9).

Click to reveal answer
intermediate
What happens if the number of parameters in @CsvSource does not match the test method parameters?

The test will fail to run because JUnit cannot map the CSV values to the method parameters correctly. The number of CSV values per line must match the number of method parameters.

Click to reveal answer
intermediate
How can you include a comma inside a value in @CsvSource?

Wrap the value containing a comma in double quotes inside the CSV string. For example: @CsvSource({"\"Hello, World\"", 5}) treats Hello, World as one parameter.

Click to reveal answer
beginner
Show a simple example of a JUnit test method using @CsvSource.
  @ParameterizedTest
  @CsvSource({"2, 3, 5", "4, 5, 9"})
  void testAdd(int a, int b, int expected) {
    assertEquals(expected, a + b);
  }

This runs the test twice with different inputs and checks the sum.

Click to reveal answer
What does @CsvSource provide for a JUnit test?
AInline CSV data for multiple test runs
BExternal CSV file loading
CDatabase connection for tests
DMocking objects automatically
How do you separate multiple test cases inside @CsvSource?
AUsing semicolons
BUsing commas inside a single string
CUsing multiple strings separated by commas
DUsing new lines
If a test method has three parameters, how many values should each CSV line have?
AThree
BOne
CTwo
DFour
How do you include a comma inside a CSV value in @CsvSource?
AEscape with a backslash
BWrap the value in double quotes
CUse a semicolon instead
DYou cannot include commas
Which annotation must be used with @CsvSource to run parameterized tests?
A@Test
B@BeforeEach
C@AfterAll
D@ParameterizedTest
Explain how @CsvSource works in JUnit parameterized tests.
Think about how you can run the same test with different inputs easily.
You got /4 concepts.
    Describe how to format CSV values inside @CsvSource when a value contains a comma.
    Remember how CSV files handle commas inside values.
    You got /3 concepts.