0
0
JUnittesting~10 mins

Why patterns improve test quality 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 create a test method using JUnit 5.

JUnit
@Test
void [1]() {
    assertEquals(4, 2 + 2);
}
Drag options to blanks, or click blank then click option'
AadditionTest
BsumTest
CtestAddition
DcheckSum
Attempts:
3 left
💡 Hint
Common Mistakes
Using vague method names like 'test1' or 'check'.
Not using the @Test annotation.
2fill in blank
medium

Complete the code to use the Arrange-Act-Assert pattern in the test.

JUnit
@Test
void testMultiply() {
    // Arrange
    int a = 3;
    int b = 5;

    // Act
    int result = a [1] b;

    // Assert
    assertEquals(15, result);
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Skipping the Arrange-Act-Assert structure.
3fill in blank
hard

Fix the error in the test assertion to correctly check for null.

JUnit
@Test
void testObjectIsNull() {
    Object obj = null;
    assert[1](obj);
}
Drag options to blanks, or click blank then click option'
ATrue
BEquals
CNotNull
DNull
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertNotNull when the object is null.
Using assertEquals instead of assertNull.
4fill in blank
hard

Fill both blanks to use the Given-When-Then pattern in comments.

JUnit
@Test
void testDivide() {
    // [1]
    int numerator = 10;
    int denominator = 2;

    // [2]
    int result = numerator / denominator;

    assertEquals(5, result);
}
Drag options to blanks, or click blank then click option'
AGiven
BArrange
CWhen
DAct
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up Arrange/Act with Given/When.
Not using clear comments to separate test steps.
5fill in blank
hard

Fill all three blanks to create a parameterized test using JUnit 5.

JUnit
@ParameterizedTest
@ValueSource(ints = {2, 4, 6})
void testIsEven([1] number) {
    // [2]
    boolean isEven = number % 2 [3] 0;
    assertTrue(isEven);
}
Drag options to blanks, or click blank then click option'
Aint
BArrange
C==
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter types.
Omitting comments that clarify test steps.
Using assignment '=' instead of equality '==' in condition.