Using multiple parameter types lets you test a method with different kinds of inputs in one test. This helps find bugs faster and saves time.
Multiple parameter types in JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; class TestClass { @ParameterizedTest @CsvSource({ "input1, 10", "input2, 20" }) void testMethod(String param1, int param2) { // test code here } }
@ParameterizedTest marks the test to run multiple times with different inputs.
@CsvSource provides pairs of values separated by commas for each test run.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; class FruitTest { @ParameterizedTest @CsvSource({ "apple, 5", "banana, 6" }) void testFruitLength(String fruit, int length) { assertEquals(length, fruit.length()); } }
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; class SumTest { @ParameterizedTest @CsvSource({ "10, 20, 30", "5, 15, 20" }) void testSum(int a, int b, int expectedSum) { assertEquals(expectedSum, a + b); } }
This test runs the add method with three sets of integer inputs and checks if the sum is correct each time.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; public class CalculatorTest { @ParameterizedTest @CsvSource({ "2, 3, 5", "10, 20, 30", "5, 7, 12" }) void testAdd(int a, int b, int expected) { Calculator calc = new Calculator(); assertEquals(expected, calc.add(a, b)); } } class Calculator { int add(int x, int y) { return x + y; } }
Use @CsvSource for simple multiple parameters separated by commas.
Ensure parameter types in the test method match the types in the CsvSource.
Each line in @CsvSource represents one test run with its parameters.
Multiple parameter types let you test methods with different inputs easily.
@ParameterizedTest with @CsvSource is a simple way to provide multiple inputs.
This approach saves time and improves test coverage.
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
