Multiple parameter types in JUnit - Build an Automation Script
import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; public class CalculatorTest { static class Calculator { public double add(int a, double b) { return a + b; } } private static Stream<org.junit.jupiter.params.provider.Arguments> provideParameters() { return Stream.of( org.junit.jupiter.params.provider.Arguments.of(5, 3.2, 8.2), org.junit.jupiter.params.provider.Arguments.of(-1, 0.0, -1.0), org.junit.jupiter.params.provider.Arguments.of(0, -2.5, -2.5) ); } @ParameterizedTest(name = "add({0}, {1}) = {2}") @MethodSource("provideParameters") void testAddWithMultipleParameterTypes(int a, double b, double expected) { Calculator calculator = new Calculator(); double result = calculator.add(a, b); assertEquals(expected, result, 0.0001, "Sum should be correct"); } }
This test uses JUnit 5's @ParameterizedTest with @MethodSource to supply multiple sets of parameters with different types: an int and a double. The Calculator class has an add method that takes these two parameters and returns their sum as a double.
The provideParameters method returns a stream of arguments with three test cases. The test method testAddWithMultipleParameterTypes receives these parameters, calls the add method, and asserts the result is equal to the expected sum using assertEquals with a small delta to handle floating point precision.
This approach keeps the test clean, readable, and easily extendable for more cases.
Now add data-driven testing with 3 different inputs including zero and negative values
Practice
@ParameterizedTest with multiple parameter types in JUnit?Solution
Step 1: Understand the purpose of @ParameterizedTest
@ParameterizedTest lets you run one test method multiple times with different inputs.Step 2: Recognize the benefit of multiple parameter types
Using multiple parameter types means you can test various input combinations easily, improving coverage.Final Answer:
It allows running the same test method with different input values easily. -> Option CQuick Check:
@ParameterizedTest runs tests with many inputs = A [OK]
- Confusing parameterized tests with automatic bug fixing
- Believing it skips assertions to run faster
- Assuming it generates reports without code
@CsvSource in JUnit?Solution
Step 1: Recall the correct @CsvSource format
@CsvSource expects an array of strings, each string with comma-separated values.Step 2: Check each option's syntax
@CsvSource({"1, 'test', true", "2, 'check', false"}) uses curly braces and comma-separated values inside quotes, which is correct syntax.Final Answer:
@CsvSource({"1, 'test', true", "2, 'check', false"}) -> Option AQuick Check:
@CsvSource uses braces and comma-separated strings = C [OK]
- Using semicolons instead of commas
- Not enclosing the whole list in braces
- Using square brackets instead of braces
@ParameterizedTest
@CsvSource({"2, 3, 5", "4, 5, 9", "1, 1, 3"})
void testSum(int a, int b, int expected) {
assertEquals(expected, a + b);
}Solution
Step 1: Check each test case's expected vs actual sum
Test 1: 2+3=5 matches expected 5 (pass). Test 2: 4+5=9 matches expected 9 (pass). Test 3: 1+1=2 but expected 3 (fail).Step 2: Determine overall test results
Two tests pass, one test fails due to wrong expected value.Final Answer:
Two tests pass, one test fails. -> Option DQuick Check:
Sum matches expected except last = A [OK]
- Assuming all pass without checking expected values
- Ignoring the failing test case
- Thinking syntax error occurs due to quotes
@ParameterizedTest
@CsvSource({"10, 20", "30, 40"})
void testMultiply(int a, int b, int expected) {
assertEquals(expected, a * b);
}Solution
Step 1: Compare method parameters with @CsvSource values
The method expects three parameters (int a, int b, int expected), but each @CsvSource entry has only two values.Step 2: Identify mismatch causing error
Missing the third parameter in @CsvSource causes a runtime error due to parameter count mismatch.Final Answer:
The method expects three parameters but only two are provided in @CsvSource. -> Option BQuick Check:
Parameter count mismatch = B [OK]
- Thinking commas are wrong separators
- Assuming assertEquals argument order causes error
- Believing @ParameterizedTest can't be used with @CsvSource
isValidUser(String username, int age, boolean active) using @ParameterizedTest with multiple parameter types. Which @CsvSource entry correctly represents a test case with username "alice", age 30, and active status true?Solution
Step 1: Understand @CsvSource value format
Values are comma-separated strings; quotes around strings are optional unless spaces or commas inside.Step 2: Evaluate each option
"alice, 30, true" correctly uses commas without extra quotes. "'alice', 30, true" adds unnecessary quotes around username. "alice; 30; true" uses semicolons which is invalid. "alice, '30', 'true'" quotes numeric and boolean values unnecessarily.Final Answer:
"alice, 30, true" -> Option AQuick Check:
Comma-separated values without extra quotes = D [OK]
- Using semicolons instead of commas
- Adding quotes around numbers or booleans
- Quoting strings unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
