Bird
Raised Fist0
JUnittesting~10 mins

Why coverage measures test completeness in JUnit - Test Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. What does code coverage measure in JUnit testing?
easy
A. The memory used during testing
B. How much of the code is executed by tests
C. The number of test cases written
D. The speed of test execution

Solution

  1. Step 1: Understand the meaning of coverage

    Coverage shows which parts of the code are run when tests execute.
  2. Step 2: Relate coverage to test completeness

    Higher coverage means more code is tested, indicating more complete tests.
  3. Final Answer:

    How much of the code is executed by tests -> Option B
  4. Quick Check:

    Coverage = executed code portion [OK]
Hint: Coverage = code run by tests, not test count [OK]
Common Mistakes:
  • Confusing coverage with test speed
  • Thinking coverage counts test cases
  • Assuming coverage measures memory use
2. Which JUnit annotation is used to mark a method as a test to be included in coverage?
easy
A. @Test
B. @AfterEach
C. @BeforeAll
D. @Ignore

Solution

  1. Step 1: Identify JUnit test method annotation

    The @Test annotation marks methods as test cases to run.
  2. Step 2: Understand coverage relation

    Only methods run as tests contribute to coverage measurement.
  3. Final Answer:

    @Test -> Option A
  4. Quick Check:

    @Test marks test methods [OK]
Hint: Look for @Test to include method in coverage [OK]
Common Mistakes:
  • Using @BeforeAll as test method
  • Confusing @Ignore with test execution
  • Thinking @AfterEach runs tests
3. Given this JUnit test code snippet, what is the coverage result for method calculate?
public class Calculator {
  public int calculate(int x) {
    if (x > 0) return x * 2;
    else return 0;
  }
}

@Test
public void testCalculatePositive() {
  Calculator c = new Calculator();
  assertEquals(4, c.calculate(2));
}
medium
A. 50% coverage of calculate method
B. 100% coverage of calculate method
C. 0% coverage of calculate method
D. 75% coverage of calculate method

Solution

  1. Step 1: Analyze test input and branches

    The test calls calculate(2), which triggers the if branch (x > 0).
  2. Step 2: Check coverage of else branch

    The else branch (return 0) is never executed, so not covered.
  3. Final Answer:

    50% coverage of calculate method -> Option A
  4. Quick Check:

    One branch covered, one not = 50% [OK]
Hint: Test all branches for full coverage [OK]
Common Mistakes:
  • Assuming one test covers all branches
  • Ignoring else branch coverage
  • Confusing coverage with test success
4. Identify the problem in this JUnit test that causes incomplete coverage of method isValid:
public boolean isValid(int n) {
  if (n > 0) return true;
  else return false;
}

@Test
public void testIsValid() {
  assertTrue(isValid(1));
}
medium
A. Test method missing @Test annotation
B. Test uses assertTrue incorrectly
C. Method isValid has syntax error
D. Test does not check negative input branch

Solution

  1. Step 1: Review test input coverage

    The test only calls isValid(1), covering the positive branch.
  2. Step 2: Check missing branch coverage

    The negative or zero input branch (else) is not tested, causing incomplete coverage.
  3. Final Answer:

    Test does not check negative input branch -> Option D
  4. Quick Check:

    Missing else branch test = incomplete coverage [OK]
Hint: Test all input types to cover branches [OK]
Common Mistakes:
  • Assuming one positive test covers all
  • Thinking assertTrue usage affects coverage
  • Ignoring missing @Test annotation
5. You have a method with multiple conditions:
public String classify(int age) {
  if (age < 13) return "child";
  else if (age < 20) return "teen";
  else return "adult";
}
To achieve 100% coverage, which set of test inputs should you include in your JUnit tests?
hard
A. 0, 12, 20
B. 10, 18
C. 5, 15, 25
D. 15, 25

Solution

  1. Step 1: Identify all branches in classify method

    There are three branches: age < 13, 13 ≤ age < 20, and age ≥ 20.
  2. Step 2: Select test inputs covering all branches

    Inputs 5 (child), 15 (teen), and 25 (adult) cover all branches fully.
  3. Final Answer:

    5, 15, 25 -> Option C
  4. Quick Check:

    One input per branch = full coverage [OK]
Hint: Test one value per branch for full coverage [OK]
Common Mistakes:
  • Missing one branch input
  • Using boundary values without covering all branches
  • Assuming fewer tests cover all cases
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard