Quick revision
The short answer
We annotate the method with @ParameterizedTest and @CsvSource, give it one row per case such as "a@b.com, true" and "bad, false", declare the method as void validates(String input, boolean expected), and assert assertEquals(expected, validator.isValidEmail(input)). The name attribute gives each case a readable display name, and a separate test with @NullAndEmptySource covers null and empty input.
What to remember
In this answer, we wrote a parameterized test with @ParameterizedTest and @CsvSource, one row per case with the input and the expected boolean, and a method validates(String input, boolean expected) that asserts assertEquals(expected, validator.isValidEmail(input)). We used the name attribute for readable display names, saw how CSV values are trimmed, quoted and converted, and added a single-parameter test with @NullAndEmptySource for null and empty input.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We annotate the method with @ParameterizedTest and @CsvSource, give it one row per case such as "a@b.com, true" and "bad, false", declare the method as void validates(String input, boolean expected), and assert assertEquals(expected, validator.isValidEmail(input)). The name attribute gives each case a readable display name, and a separate test with @NullAndEmptySource covers null and empty input.
Why this question is asked
Validation rules are a perfect fit for parameterized tests: the logic is the same and only the input and the expected answer change. Writing six nearly identical test methods hides the cases in noise, and a loop inside one test stops at the first failure. Interviewers ask for this small piece of code to see whether we know the JUnit API well enough to write it without looking it up, and whether we think about edge cases. In this answer, we will see what a parameterized test is, write the @CsvSource test step by step, give it readable names, and add the null and empty cases.
1A Quick Recap of Parameterized Tests
A parameterized test is one test method that JUnit runs several times with different arguments. Instead of @Test we use @ParameterizedTest, and we add a source annotation that supplies the arguments. Each set of arguments becomes its own test invocation in the report, so one failing case does not hide the others.
@CsvSource is the simplest source when each case has a few simple values. Each string in it is one row of comma-separated values, and JUnit converts each value to the type of the matching method parameter, so "true" becomes a boolean and "42" becomes an int. Parameterized tests live in the junit-jupiter-params artifact, which the junit-jupiter aggregate dependency already includes.
2Writing the Test
Here is the test with three valid and three invalid addresses. The method takes two parameters in the same order as the columns: the input and the expected result.
Notice a few details. JUnit trims spaces around unquoted values, so we can line up the columns for readability. The second column is converted to a boolean automatically. And the assertion compares the expected value with the actual one, in that order, so a failure message reads expected <false> but was <true>.
If a value contains a comma, we wrap it in single quotes, for example "'a,b@c.com', false". An empty quoted value '' is an empty string, while a completely empty unquoted value becomes null.
3Readable Display Names
By default the report shows each invocation with its index and arguments, which is already readable for short values. The name attribute lets us choose the format. {index} is the invocation number, {0} and {1} are the arguments by position, and {arguments} is all of them. With name = "[{index}] {0} -> {1}", a failure shows up as [4] bad -> false, so we know immediately which case broke without reading the stack trace.
For tests where the raw values are cryptic, we can add a third column with a description and use it in the name, or use a header row with @CsvSource(useHeadersInDisplayName = true, ...), so the display name includes the column names; check your JUnit version for that option.
4Adding Null and Empty Cases
Email fields are often empty or missing, so a good test covers null and "". @NullAndEmptySource supplies those two values, but it only works for methods with a single parameter, because it cannot invent an expected value for a second one. So we put the invalid-only cases in their own test that takes just the input and asserts false.
| Source | Supplies | Good for |
|---|---|---|
@ValueSource | one value per invocation | single-parameter checks |
@CsvSource | rows of several values | input plus expected result |
@CsvFileSource | rows from a CSV file | long tables of cases |
@NullAndEmptySource | null and empty | blank input, one parameter only |
@MethodSource | objects from a factory method | complex arguments |
This also tests something easy to forget: that isValidEmail returns false for null instead of throwing a NullPointerException. If the rule should throw for null instead, the test makes that decision visible.
5Follow-up questions
How many tests does JUnit report for this class?
Eleven invocations: six from the @CsvSource test and five from the second test, two from @NullAndEmptySource and three from @ValueSource. Each is reported and can fail on its own, grouped under its parameterized method.
Why not loop over the cases inside one @Test?
Because the first failing case throws and stops the loop, so the other cases are not checked, and the report shows a single test instead of one entry per case. Parameterized tests give each case its own result, its own name and its own lifecycle callbacks.
Would you test email validation with only six cases?
For an interview answer, six shows the technique. For real code I would add cases for the rules that actually matter to the business, such as plus addressing, subdomains, uppercase letters and very long addresses, and I would keep each rule's cases readable rather than adding hundreds of random strings.
6Conclusion
In this answer, we wrote a parameterized test with @ParameterizedTest and @CsvSource, one row per case with the input and the expected boolean, and a method validates(String input, boolean expected) that asserts assertEquals(expected, validator.isValidEmail(input)). We used the name attribute for readable display names, saw how CSV values are trimmed, quoted and converted, and added a single-parameter test with @NullAndEmptySource for null and empty input.
Open with
I would write one method annotated with @ParameterizedTest and @CsvSource, where each row is an input and the expected result, for example a@b.com comma true, x@y.org comma true, first.last@co.uk comma true, and then bad, no-at.example and two@@signs.com with false.
Then, if they want more
The method signature is void validates(String input, boolean expected), and JUnit converts the second column to a boolean for me. The body is just assertEquals(expected, validator.isValidEmail(input)), with expected first so the failure message reads correctly.
I would set the name attribute to something like bracket index, then {0} arrow {1}, so a failure in the report shows exactly which address broke. Values are trimmed, so I can align the columns, and if a value contains a comma I wrap it in single quotes.
For null and empty input I would add a second test that takes only the input and asserts false, annotated with @NullAndEmptySource, because that source only works with a single parameter, plus @ValueSource for blank and partial strings like spaces or a@.
The advantage over a loop is that each case is its own test, so one failure does not hide the others.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a JUnit 5 parameterized test that checks an isValidEmail method with three valid...": https://scrivio-interview-library.netlify.app/java/junit/5687b7570b/