Building on the second post on jUnit 6, here is a parametric unit test set up with an external CSV file:
# -----------------
# operand, operand, answer of summation
# -----------------
operand1,operand2,expected
1,2,3
4,5,9
7,8,15
# Nothing after this.
This file, test values.csv, has comments and a header line that is to be ignored. It can be included in an IntelliJ project. We assume that a Calculator class is available that contains an add() method.
now, the unit test file needs to be able to import the CSV file and apply it. Like this:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
// Source: https://ankurm.com/junit-6-parameterized-tests/
// Source: https://davidvlijmincx.com/posts/junit-parameters-csv-file/
public class testFile4ParamExtCSV {
// The following is a parameterized test. Not an @Test.
@ParameterizedTest(name = "{0} + {1} = {2}") // Display the name while testing.
@CsvFileSource(resources ="/testvalues.csv", numLinesToSkip = 1,encoding = "UTF-8" )
// 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);
}
}
We've now got @CsvFileSource() to import that CSV file. The rest is as before.
Sidebar: a thank you
As I work my way through jUnit 6 I'm reminded of the conversation that I had with Professor Emeritus Jonathan Ostroff, prior to the pandemic, as I prepared to start teaching Java and object-oriented programming for the first time. He patiently walked me through the concept of a unit test as a formal process in software engineering. I'm sure glad that he took the time to do so.

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.
