0
0
JUnittesting~10 mins

Test class organization 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 declare a JUnit test class.

JUnit
public class [1] {

}
Drag options to blanks, or click blank then click option'
Acalculator
Bcalculate
CTestCalculator
DCalculatorTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names or lowercase names as class names.
Not indicating the class is a test class.
2fill in blank
medium

Complete the code to import the JUnit 5 test annotation.

JUnit
import org.junit.jupiter.api.[1];
Drag options to blanks, or click blank then click option'
ATest
BBeforeEach
CTestCase
DTestRunner
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'TestCase' which is from older JUnit versions.
Confusing setup annotations with test annotations.
3fill in blank
hard

Fix the error in the test method declaration by completing the annotation.

JUnit
public class CalculatorTest {

    [1]
    public void addNumbers() {
        // test code
    }
}
Drag options to blanks, or click blank then click option'
A@TestMethod
B@Test
C@TestCase
D@TestRun
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect annotation names like '@TestMethod' or '@TestCase'.
Forgetting the '@' symbol.
4fill in blank
hard

Fill both blanks to declare a setup method that runs before each test.

JUnit
public class CalculatorTest {

    [1]
    void [2]() {
        // setup code
    }
}
Drag options to blanks, or click blank then click option'
A@BeforeEach
Bsetup
Cinitialize
D@BeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@BeforeAll' which runs once before all tests, not before each.
Using method names that do not describe setup.
5fill in blank
hard

Fill all three blanks to write a test method that asserts two values are equal.

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

public class CalculatorTest {

    @Test
    void testAddition() {
        int expected = 5;
        int actual = 2 + 3;
        [2]([3], actual);
    }
}
Drag options to blanks, or click blank then click option'
AassertEquals
BassertTrue
Cexpected
DassertFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'assertTrue' or 'assertFalse' instead of 'assertEquals' for equality check.
Swapping expected and actual arguments.