Skip to main content Skip to local navigation

jUnit 6, part 2: parametric testing with delimiters and comments

testing icon from The Noun Project.

I started examining jUnit 6 in an earlier post. It turns out that in jUnit6, the common separated values data can use delimiters other than commas. Very cool. Before getting into the import of CSV files for testing, let's look at two features that are useful for setting up a bank of tests. We'll assume that there is a Calculator class that contains a method called add() and that it is available in a Java project. It will be the target of the test.

As with the earlier post, I'm using Ankurm as the original source material for this second post.

Example: CSV text with custom delimiter and comments

The following example shows a CSV block divided into rows and columns. Each row is a new pair of inputs and out output, read from left to right. Rather than use commas to delimit, I'm using an ellipsis (three dots). Also, I think that it's important to include comments in my CSV data. It turns out that the CSV parser for jUnit6 is FastCSV and it supports comments with hashmarks -- but only if the hashmark is the first character on a line. Kind of limiting, but acceptable. Here's the unit test class, testFile3ParamCSV.

public class testFile3ParamCSV {
    // The following is a parameterized test.  Not an @Test.
    @ParameterizedTest(name = "{0} + {1} = {2}")        // Display the name while testing.
    @CsvSource( delimiterString = "...",
                textBlock = """
# valid comment
                            1...2...3
                            4...5...9
                            7...8...10
# another comment
            """)
    // This is the test method.
    void testMethod1(int In_A, int In_B, int expectedSum) {
        Calculator calculator = new Calculator();
        assertEquals(expectedSum, calculator.add(In_A, In_B),
                In_A + " + " + In_B + " should equal " + expectedSum);
    }
}

The CSV data has five lines. The first and last lines are comments as they start with hashmarks. The middle three lines are the three rows of CSV data. You will have noticed by now that there are three dots between each number. The three dots are the replacement for the comma in a typical CSV file.

The first two columns are the inputs to an addition and the last column is the known good answer of the addition. Together, these form the reference information for the unit test. The known good answer will be compared to the answer that comes out of the calculator method. Now, it's important to note that the first and second rows of CSV data have correct answers. The third one, in which I claim that 7+8 = 10 is, clearly, incorrect. I did that on purpose so that the unit test process would claim that the third set of parameterized data was wrong, as shown below:

jUnit6 test that shows that there is a problem with the third test in my parameterized set of tests.
jUnit6 test that shows that there is a problem with the third test in my parameterized set of tests.

Just as important as it finding that error, this test class also demonstrates that comments are permissible in the CSV data and that the use of non-commas is also possible, even if they are multi-character, as I've done above.


a pen

James Andrew Smith is a Professional Engineer and Associate Professor in the Electrical Engineering and Computer Science Department of York University’s Lassonde School, with degrees in Electrical and Mechanical Engineering from the University of Alberta and McGill University.  Previously a program director in biomedical engineering, his research background spans robotics, locomotion, human birth, music and engineering education. While on sabbatical in 2018-19 with his wife and kids he lived in Strasbourg, France and he taught at the INSA Strasbourg and Hochschule Karlsruhe and wrote about his personal and professional perspectives.  James is a proponent of using social media to advocate for justice, equity, diversity and inclusion as well as evidence-based applications of research in the public sphere. You can find him on Twitter.  You can find him on BlueSky. Originally from Québec City, he now lives in Toronto, Canada.