0
0
JUnittesting~10 mins

Why CI integration enables continuous quality 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 run a simple JUnit test method.

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

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
B6
C4
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong expected value in assertEquals.
Confusing actual and expected parameters.
2fill in blank
medium

Complete the code to annotate a method as a JUnit test.

JUnit
import org.junit.jupiter.api.Test;

public class SampleTest {
    [1]
    public void sampleMethod() {
        // test code here
    }
}
Drag options to blanks, or click blank then click option'
A@BeforeEach
B@Disabled
C@Test
D@AfterAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using setup or teardown annotations instead of @Test.
Forgetting to annotate the test method.
3fill in blank
hard

Fix the error in the assertion to correctly check string equality.

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

public class StringTest {
    @Test
    public void testGreeting() {
        String greeting = "Hello";
        assertEquals([1], greeting);
    }
}
Drag options to blanks, or click blank then click option'
A"Hello"
B"hello"
Cgreeting.toLowerCase()
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different case in the expected string.
Passing null or unrelated strings.
4fill in blank
hard

Fill both blanks to create a test that fails if the number is not positive.

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

public class NumberTest {
    @Test
    public void testPositive() {
        int number = -5;
        assertTrue(number [1] [2]);
    }
}
Drag options to blanks, or click blank then click option'
A>
B0
C<
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than.
Comparing to -1 instead of 0.
5fill in blank
hard

Fill all three blanks to create a test that checks if a list contains a specific element.

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

public class ListTest {
    @Test
    public void testContains() {
        List<String> fruits = List.of("apple", "banana", "cherry");
        assertTrue(fruits.[1]([2]), "List should contain [3].");
    }
}
Drag options to blanks, or click blank then click option'
Acontains
B"banana"
Cbanana
D"apple"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the element without quotes causing a compile error.
Using the wrong method name.
Mismatch between the element checked and the message.