Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Before or @After instead of @Test.
Forgetting the '@' symbol.
✗ Incorrect
The @Test annotation marks a method as a test method in JUnit.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @Before or @After instead of @Test.
Missing the import statement.
✗ Incorrect
The @Test annotation is imported from org.junit.Test in JUnit 4.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Writing 'Test' without '@'.
Using lowercase '@test'.
Using '@TestMethod' which is not a JUnit annotation.
✗ Incorrect
The correct annotation is '@Test' with uppercase 'T' and the '@' symbol.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue or assertFalse which do not compare two values directly.
Using assertNull which checks for null values.
✗ Incorrect
assertEquals(expected, actual) checks if the two values are equal in JUnit tests.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The test expects MyException.class, throws new MyException(), and defines MyException extending RuntimeException.