0
0
JUnittesting~10 mins

Why coverage measures test completeness 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 JUnit test method that checks if a number is positive.

JUnit
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

public class NumberTest {
    @Test
    public void testPositive() {
        int number = 5;
        assertTrue(number [1] 0);
    }
}
Drag options to blanks, or click blank then click option'
A==
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes the test to fail for positive numbers.
2fill in blank
medium

Complete the code to assert that a string is not null in a JUnit test.

JUnit
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;

public class StringTest {
    @Test
    public void testStringNotNull() {
        String text = "hello";
        assertNotNull([1]);
    }
}
Drag options to blanks, or click blank then click option'
Atext
Bnull
C"hello"
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'null' or a string literal instead of the variable causes the test to fail.
3fill in blank
hard

Fix the error in the JUnit test assertion that checks equality of two integers.

JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class EqualityTest {
    @Test
    public void testEqualNumbers() {
        int expected = 10;
        int actual = 5 + 5;
        assertEquals([1], actual);
    }
}
Drag options to blanks, or click blank then click option'
A10
Bactual
C5
Dexpected
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping expected and actual arguments causes confusing test results.
4fill in blank
hard

Fill both blanks to complete a JUnit test that checks if a list contains a specific element.

JUnit
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import org.junit.jupiter.api.Test;

public class ListTest {
    @Test
    public void testListContains() {
        List<String> fruits = List.of("apple", "banana", "cherry");
        assertTrue(fruits.[1]("banana") [2] true);
    }
}
Drag options to blanks, or click blank then click option'
Acontains
Bequals
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'equals' instead of 'contains' or wrong comparison operators.
5fill in blank
hard

Fill all three blanks to complete a JUnit test that verifies a map contains a key and its value is correct.

JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Map;
import org.junit.jupiter.api.Test;

public class MapTest {
    @Test
    public void testMapKeyValue() {
        Map<String, Integer> ages = Map.of("Alice", 30, "Bob", 25);
        assertEquals([1], ages.get([2]));
        assertEquals([3], ages.size());
    }
}
Drag options to blanks, or click blank then click option'
A30
B"Alice"
C2
D"Bob"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys or values, or incorrect map size in assertions.