0
0
JUnittesting~10 mins

Test result publishing 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 publish the test result using JUnit's assertion.

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

public class CalculatorTest {
    @Test
    public void testAddition() {
        int result = 2 + 3;
        assertEquals([1], result);
    }
}
Drag options to blanks, or click blank then click option'
A7
B4
C5
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using the actual result as the expected value.
Mixing up the order of expected and actual in assertEquals.
2fill in blank
medium

Complete the code to publish a test failure when the condition is false.

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

public class NumberTest {
    @Test
    public void testIsPositive() {
        int number = -1;
        assertTrue([1]);
    }
}
Drag options to blanks, or click blank then click option'
Anumber < 0
Bnumber > 0
Cnumber == 0
Dnumber >= 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that is false for the given number.
Confusing greater than and less than operators.
3fill in blank
hard

Fix the error in the test result publishing code by completing the assertion.

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

public class StringTest {
    @Test
    public void testStringLength() {
        String text = "hello";
        assertEquals([1], text.length());
    }
}
Drag options to blanks, or click blank then click option'
A5
B6
C4
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the actual length as the expected value.
Off-by-one errors in counting string length.
4fill in blank
hard

Fill both blanks to publish test results for checking if a list contains a specific element.

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

public class ListTest {
    @Test
    public void testContainsElement() {
        List<String> fruits = List.of("apple", "banana", "cherry");
        assertTrue(fruits.[1]([2]));
    }
}
Drag options to blanks, or click blank then click option'
Acontains
B"banana"
C"orange"
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that modifies the list instead of checking.
Checking for an element not in the list.
5fill in blank
hard

Fill all three blanks to publish a test result that verifies a map contains a key with a specific value.

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

public class MapTest {
    @Test
    public void testMapEntry() {
        Map<String, Integer> scores = Map.of("Alice", 90, "Bob", 85);
        assertEquals([1], scores.get([2]));
        assertTrue(scores.[3]([2]));
    }
}
Drag options to blanks, or click blank then click option'
A90
B"Alice"
CcontainsKey
D85
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up keys and values in the map.
Using a method that checks values instead of keys.