0
0
JUnittesting~10 mins

Why JUnit is the standard for Java testing - 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
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {
    @[1]
    void testAddition() {
        int result = 2 + 3;
        assertEquals(5, result);
    }
}
Drag options to blanks, or click blank then click option'
AIgnore
BBeforeEach
CAfterAll
DTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @Test for test methods.
Forgetting the @Test annotation entirely.
2fill in blank
medium

Complete the assertion to check if two strings are equal.

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

public class StringTest {
    @Test
    void testEquality() {
        String expected = "hello";
        String actual = "hello";
        [1](expected, actual);
    }
}
Drag options to blanks, or click blank then click option'
AassertNull
BassertTrue
CassertEquals
DassertSame
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue with a direct comparison instead of assertEquals.
Using assertSame which checks object identity, not equality.
3fill in blank
hard

Fix the error in the test method to correctly expect an exception.

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

public class ExceptionTest {
    @Test
    void testException() {
        assertThrows([1].class, () -> {
            Integer.parseInt("abc");
        });
    }
}
Drag options to blanks, or click blank then click option'
ANumberFormatException
BIllegalArgumentException
CNullPointerException
DIOException
Attempts:
3 left
💡 Hint
Common Mistakes
Using NullPointerException which is unrelated here.
Using IOException which is for input/output errors.
4fill in blank
hard

Fill in the blank to create a parameterized test that runs with multiple inputs.

JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ParameterizedTestExample {
    @[1]
    @ValueSource(strings = {"racecar", "radar", "level"})
    void testPalindrome(String word) {
        assertTrue(word.equals(new StringBuilder(word).reverse().toString()));
    }
}
Drag options to blanks, or click blank then click option'
ATest
BParameterizedTest
CBeforeAll
DAfterEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of @ParameterizedTest for parameterized tests.
Missing the @ParameterizedTest annotation.
5fill in blank
hard

Fill all three blanks to create a test suite that runs multiple test classes.

JUnit
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses([1].class, [2].class)
public class [3] {
}
Drag options to blanks, or click blank then click option'
ACalculatorTest
BStringTest
CAllTests
DExceptionTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using test class names not imported or defined.
Naming the suite class incorrectly.