0
0
JUnittesting~10 mins

Why parameterization reduces test duplication in JUnit - Test Your Understanding

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

Complete the code to use the correct annotation for parameterized tests in JUnit.

JUnit
@[1]
public void testSum(int a, int b, int expected) {
    assertEquals(expected, a + b);
}
Drag options to blanks, or click blank then click option'
AParameterizedTest
BAfterEach
CTest
DBeforeEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of @ParameterizedTest causes the test to run only once.
Using lifecycle annotations like @BeforeEach instead of test annotations.
2fill in blank
medium

Complete the code to provide multiple sets of parameters using the correct source annotation.

JUnit
@ParameterizedTest
@[1]({"1, 2, 3", "3, 4, 7", "5, 6, 11"})
public void testSum(int a, int b, int expected) {
    assertEquals(expected, a + b);
}
Drag options to blanks, or click blank then click option'
AValueSource
BEnumSource
CMethodSource
DCsvSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ValueSource which supports only single parameter arrays.
Using @MethodSource without defining a method.
3fill in blank
hard

Fix the error in the parameterized test method signature to match the parameters provided.

JUnit
@ParameterizedTest
@CsvSource({"2, 3, 5", "4, 5, 9"})
public void testSum([1]) {
    assertEquals(expected, a + b);
}
Drag options to blanks, or click blank then click option'
Aint a, int b, int expected
Bint a, int b
CString a, String b, String expected
Dint expected
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the expected parameter causes compilation errors.
Using wrong parameter types like String instead of int.
4fill in blank
hard

Fill both blanks to correctly import and use the assertion method in JUnit 5.

JUnit
import static org.junit.jupiter.api.Assertions.[1];

@Test
public void testAddition() {
    [2](5, 2 + 3);
}
Drag options to blanks, or click blank then click option'
AassertEquals
BassertTrue
CassertFalse
DassertNull
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue or assertFalse when checking equality.
Forgetting to import the assertion method.
5fill in blank
hard

Fill all three blanks to complete a parameterized test that uses a method source for input data.

JUnit
@ParameterizedTest
@MethodSource("[1]")
public void testMultiply(int a, int b, int expected) {
    assertEquals([2], a * b);
}

static Stream<Arguments> [3]() {
    return Stream.of(
        Arguments.of(2, 3, 6),
        Arguments.of(4, 5, 20),
        Arguments.of(6, 7, 42)
    );
}
Drag options to blanks, or click blank then click option'
AmultiplyData
Bexpected
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching method source name causes runtime errors.
Using wrong variable names in assertion.