0
0
JUnittesting~10 mins

Line and branch coverage in JUnit - Interactive Code Practice

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 the sum method returns the correct result.

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

public class CalculatorTest {
    @Test
    public void testSum() {
        Calculator calc = new Calculator();
        int result = calc.sum(2, 3);
        assertEquals([1], result);
    }
}
Drag options to blanks, or click blank then click option'
A5
B7
C4
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong expected value in assertEquals.
Forgetting to import assertEquals.
2fill in blank
medium

Complete the code to add a test method that checks if the isPositive method returns true for positive numbers.

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

public class NumberTest {
    @Test
    public void testIsPositive() {
        NumberChecker checker = new NumberChecker();
        assertTrue(checker.isPositive([1]));
    }
}
Drag options to blanks, or click blank then click option'
A-5
B5
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Testing with zero or negative numbers expecting true.
Not importing assertTrue.
3fill in blank
hard

Fix the error in the test method to correctly check if the max method returns the larger number.

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

public class MaxTest {
    @Test
    public void testMax() {
        MaxFinder finder = new MaxFinder();
        int max = finder.max(10, 20);
        assertEquals([1], max);
    }
}
Drag options to blanks, or click blank then click option'
A30
B10
C0
D20
Attempts:
3 left
💡 Hint
Common Mistakes
Using the smaller number as expected value.
Confusing the order of parameters.
4fill in blank
hard

Fill both blanks to complete the test method that checks if the isEven method returns true for even numbers and false for odd numbers.

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

public class EvenOddTest {
    @Test
    public void testIsEven() {
        EvenOddChecker checker = new EvenOddChecker();
        assert[2](checker.isEven(4));
        assertFalse(checker.isEven(5));
    }
}
Drag options to blanks, or click blank then click option'
ATrue
BFalse
CEquals
DNull
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertFalse instead of assertTrue for even number test.
Using assertEquals incorrectly here.
5fill in blank
hard

Fill all three blanks to complete the test method that verifies the behavior of the divide method, including handling division by zero.

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

public class DivisionTest {
    @Test
    public void testDivide() {
        Calculator calc = new Calculator();
        assert[2](2, calc.divide(10, 5));
        assertThrows([3].class, () -> calc.divide(10, 0));
    }
}
Drag options to blanks, or click blank then click option'
AEquals
BassertEquals
CArithmeticException
DIllegalArgumentException
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong assertion method names.
Expecting wrong exception type for division by zero.