0
0
JUnittesting~10 mins

@ParameterizedTest annotation in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
ATest
BParameterizedTest
CBeforeEach
DAfterAll
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @Test instead of @ParameterizedTest
Using annotations from JUnit 4
Forgetting to import the annotation
2fill in blank
medium

Complete 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'
AString
Bint
Cdouble
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using int or other types instead of String
Forgetting to match parameter type with @ValueSource type
3fill in blank
hard

Fix 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'
AInteger
BString
Cint
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using String instead of int
Using wrapper Integer instead of primitive int
4fill in blank
hard

Fill 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'
Aints
BString
Cint
Dstrings
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'strings' or 'String' for integer values
Mismatch between annotation and parameter types
5fill in blank
hard

Fill 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'
AString
Bint
C0
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter types
Incorrect comparison value in assertion