0
0
JUnittesting~10 mins

Why test structure ensures clarity 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 declare a test method in JUnit 5.

JUnit
@[1]
public void testAddition() {
    int sum = 2 + 3;
    assertEquals(5, sum);
}
Drag options to blanks, or click blank then click option'
ATest
BBeforeEach
CAfterEach
DIgnore
Attempts:
3 left
💡 Hint
Common Mistakes
Using lifecycle annotations like @BeforeEach instead of @Test.
Forgetting the @Test annotation.
2fill in blank
medium

Complete the code to assert that two strings are equal in a JUnit test.

JUnit
assert[1]("hello", greeting);
Drag options to blanks, or click blank then click option'
ANull
BTrue
CEquals
DSame
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue instead of assertEquals for equality.
Mixing up expected and actual parameters.
3fill in blank
hard

Fix the error in the test method declaration by completing the annotation.

JUnit
@org.junit.jupiter.api.[1]
void checkValue() {
    assertTrue(value > 0);
}
Drag options to blanks, or click blank then click option'
ABeforeAll
BDisabled
CAfterAll
DTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using lifecycle annotations like @BeforeAll or @AfterAll instead of @Test.
Missing the annotation entirely.
4fill in blank
hard

Fill both blanks to create a test method that checks if a list is empty.

JUnit
@[1]
public void testListEmpty() {
    List<String> list = new ArrayList<>();
    assert[2](list.isEmpty());
}
Drag options to blanks, or click blank then click option'
ATest
BTrue
CFalse
DBeforeEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @Test.
Using assertFalse instead of assertTrue for checking empty list.
5fill in blank
hard

Fill all three blanks to write a test that verifies a string contains a substring.

JUnit
@[1]
public void testContains() {
    String text = "JUnit testing";
    assert[2](text.[3]("test"));
}
Drag options to blanks, or click blank then click option'
ATest
BTrue
Ccontains
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertFalse instead of assertTrue.
Using wrong method name instead of contains.