0
0
JUnittesting~10 mins

Why advanced sources handle complex data 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 basic JUnit test method.

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

Complete the code to assert that a list contains a specific element.

JUnit
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]("banana"));
    }
}
Drag options to blanks, or click blank then click option'
Aincludes
Bexists
Ccontains
Dhas
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods that do not exist on List, like 'has' or 'exists'.
Confusing the method name with similar words.
3fill in blank
hard

Fix the error in the assertion to correctly compare two objects for equality.

JUnit
public class PersonTest {
    @Test
    public void testPersonEquality() {
        Person p1 = new Person("Alice", 30);
        Person p2 = new Person("Alice", 30);
        assert[1](p1, p2);
    }
}
Drag options to blanks, or click blank then click option'
AEquals
BSame
CEqualTo
DEqualsObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertSame instead of assertEquals for object equality.
Using non-existent assertion methods.
4fill in blank
hard

Fill both blanks to create a parameterized test method that runs with multiple inputs.

JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class NumberTest {
    @ParameterizedTest
    @ValueSource([1])
    public void testIsPositive(int number) {
        assertTrue(number [2] 0);
    }
}
Drag options to blanks, or click blank then click option'
Aints = {1, 2, 3}
B{1, 2, 3}
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the type attribute (e.g., ints =) in @ValueSource.
Using >= instead of >, which would accept zero as positive.
5fill in blank
hard

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

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

public class MapTest {
    @Test
    public void testMapKeyValue() {
        Map<String, Integer> ages = Map.of("Bob", 25, "Carol", 30);
        assertTrue(ages.[1]("Bob") && ages.get("Bob") [2] [3]);
    }
}
Drag options to blanks, or click blank then click option'
AcontainsKey
B==
C25
Dequals
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' to compare Integer objects which may fail.
Using a method other than containsKey to check for keys.