0
0
JUnittesting~10 mins

@Test annotation 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 mark the method as a test using JUnit.

JUnit
public class CalculatorTest {
    [1]
    public void testAddition() {
        // test code here
    }
}
Drag options to blanks, or click blank then click option'
A@Before
B@Test
C@After
D@Ignore
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Before or @After instead of @Test.
Forgetting the '@' symbol.
2fill in blank
medium

Complete the code to import the correct JUnit annotation for tests.

JUnit
import org.junit.[1];

public class SampleTest {
    @Test
    public void example() {}
}
Drag options to blanks, or click blank then click option'
ATest
BBefore
CIgnore
DAfter
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @Before or @After instead of @Test.
Missing the import statement.
3fill in blank
hard

Fix the error in the test method annotation.

JUnit
public class TestExample {
    [1]
    public void shouldPass() {
        // test logic
    }
}
Drag options to blanks, or click blank then click option'
A@TestMethod
BTest
C@Test
D@test
Attempts:
3 left
💡 Hint
Common Mistakes
Writing 'Test' without '@'.
Using lowercase '@test'.
Using '@TestMethod' which is not a JUnit annotation.
4fill in blank
hard

Fill both blanks to complete a test method with an assertion.

JUnit
import static org.junit.Assert.[1];

public class MathTest {
    @Test
    public void testSum() {
        int result = 2 + 3;
        [2](5, result);
    }
}
Drag options to blanks, or click blank then click option'
AassertEquals
BassertTrue
CassertFalse
DassertNull
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue or assertFalse which do not compare two values directly.
Using assertNull which checks for null values.
5fill in blank
hard

Fill all three blanks to write a test method that expects an exception.

JUnit
import org.junit.Test;

public class ExceptionTest {
    @Test(expected = [1].class)
    public void testException() {
        throw new [2]();
    }

    class [3] extends RuntimeException {}
}
Drag options to blanks, or click blank then click option'
AIllegalArgumentException
BRuntimeException
CMyException
DException
Attempts:
3 left
💡 Hint
Common Mistakes
Using different exception names in the annotation and throw statement.
Using standard exceptions instead of the custom one.
Not matching the class name in the definition.