Several arguments per case

A sum test needs three values per case, not one: the two addends and the expected sum. @CsvSource writes each case as a row of a small CSV, with one argument per column, and Jupiter converts every column to the type of its parameter. This lesson checks three rows of an addition table.

@CsvSource

The annotation takes an array of strings. Each string is one row, and the commas separate the columns. The test method takes one parameter per column, in the same order.

import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; public class SumTest { @ParameterizedTest @CsvSource({"1, 3, 4", "2, 2, 4", "0, 0, 0"}) void adds(int a, int b, int sum) { assertEquals(sum, a + b); } }

Columns and types

The columns are text, and Jupiter converts each one to the type of the parameter: an int is parsed, a double is parsed, an enum is looked up by name, and a String is passed as it is. A conversion failure is an error in that case, not a compile error, and the report names the value and the target type.

A header row is not part of the syntax: every row is data. If the table has a header in the file, tell the source to skip it, which is an attribute of @CsvFileSource; for a literal source, leave the header out.

Quotes, commas and null

A value that contains the delimiter is quoted: "a,b", 2 has two columns, not three. A quote inside a quoted value is doubled, as in "say ""hi""". The delimiter itself can be changed with delimiter =

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How many arguments does a row of @CsvSource supply?

    Answer

    One per column, in order

    Every comma-separated column is converted to the parameter at the same position.

  2. How do you put a comma inside one value?

    Answer

    Quote the value

    A quoted value may contain the delimiter, and the quotes are removed before conversion.

  3. What does the literal null in a row mean?

    Answer

    A null reference for that parameter

    CsvSource reads the word null as a null reference; an empty column is an empty string.

  4. What does this program print? public class Q1 { public static void main(String[] args) { System.out.println(1 + 3); } }

    Answer

    4

    The two integers are added, and println prints 4.

  5. What does this program print? public class Q2 { public static void main(String[] args) { String row = "1, 3, 4"; System.out.println(row.split(", ").length); } }

    Answer

    3

    The string splits at the comma and space into three parts.

  6. What does this program print? public class Q3 { public static void main(String[] args) { System.out.println("2" + 2); } }

    Answer

    22

    A string joined to a number is concatenation, so the result is 22.

  7. What does this program print? public class Q4 { public static void main(String[] args) { System.out.println(Integer.parseInt("42") + 1); } }

    Answer

    43

    parseInt turns the text into 42, and the addition gives 43.

  8. What happens when a column cannot be converted to the parameter type?

    Answer

    That case ends with an error that names the value

    Conversion happens at run time, so a bad value is an error in that invocation.

  9. What does this program print? public class Q5 { public static void main(String[] args) { String[] parts = "a,b".split(","); System.out.println(parts.length); } }

    Answer

    2

    The comma splits the string into two parts.

  10. When should a table move from @CsvSource to @CsvFileSource?

    Answer

    When the table is long or shared with non-Java readers

    A file keeps a long table readable and lets a test read the same data as the product.

The code on this page was built with OpenJDK 21.0.13 (javac) and run with JUnit Platform Console Launcher 1.14.3 on Alpine Linux in a Docker container.

Article author: Arthur Isaev