Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the annotation for parameterized tests.
JUnit
import org.junit.jupiter.api.[1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @Test instead of @ParameterizedTest
Using annotations from JUnit 4
Forgetting to import the annotation
✗ Incorrect
The @ParameterizedTest annotation is imported from org.junit.jupiter.api.ParameterizedTest.
2fill in blank
mediumComplete the code to supply string values for the parameterized test.
JUnit
@ParameterizedTest
@ValueSource(strings = {"apple", "banana", "cherry"})
void testFruits([1] fruit) {
System.out.println(fruit);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int or other types instead of String
Forgetting to match parameter type with @ValueSource type
✗ Incorrect
The parameter type must match the type of values supplied by @ValueSource, which is String here.
3fill in blank
hardFix the error in the parameterized test method signature.
JUnit
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testNumbers([1] number) {
assertTrue(number > 0);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using String instead of int
Using wrapper Integer instead of primitive int
✗ Incorrect
The parameter type must be int to match the ints array in @ValueSource.
4fill in blank
hardFill both blanks to create a parameterized test that checks if numbers are even.
JUnit
@ParameterizedTest @ValueSource([1] = {2, 4, 6, 8}) void testEvenNumbers([2] number) { assertEquals(0, number % 2); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'strings' or 'String' for integer values
Mismatch between annotation and parameter types
✗ Incorrect
The @ValueSource uses ints, so the parameter type must be int.
5fill in blank
hardFill all three blanks to create a parameterized test using @CsvSource with two parameters.
JUnit
@ParameterizedTest
@CsvSource({"apple, 1", "banana, 2", "cherry, 3"})
void testFruitCount([1] fruit, [2] count) {
assertNotNull(fruit);
assertTrue(count > [3]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter types
Incorrect comparison value in assertion
✗ Incorrect
The first parameter is a String, the second is an int, and the assertion checks count > 0.