Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the JUnit Test annotation.
JUnit
import org.junit.[1]; public class SampleTest { @Test public void testExample() { // test code } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Before or @After instead of @Test
Forgetting to import the annotation
✗ Incorrect
The @Test annotation marks a method as a test method in JUnit.
2fill in blank
mediumComplete the code to assert that two values are equal.
JUnit
import static org.junit.Assert.[1]; public class SampleTest { @Test public void testSum() { int result = 2 + 3; assertEquals(5, result); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue with equality instead of assertEquals
Not importing the assertion method
✗ Incorrect
assertEquals checks if two values are equal in JUnit tests.
3fill in blank
hardFix the error in the test method to notify failure correctly.
JUnit
import static org.junit.Assert.assertEquals; public class SampleTest { @Test public void testFailNotification() { int expected = 10; int actual = 5; assertEquals([1], actual); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping expected and actual values
Using a wrong literal value
✗ Incorrect
The first argument to assertEquals is the expected value, then the actual value.
4fill in blank
hardFill both blanks to create a test that fails with a custom message.
JUnit
import static org.junit.Assert.assertEquals; public class SampleTest { @Test public void testFailWithMessage() { assertEquals([1], [2], 100); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the message in the wrong position
Using the actual value instead of expected
✗ Incorrect
The first argument is the failure message, the second is the expected value.
5fill in blank
hardFill all three blanks to write a test that fails if a condition is false with a message.
JUnit
import static org.junit.Assert.assertTrue; public class SampleTest { @Test public void testCondition() { assertTrue([1], [2] > [3]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping x and y in the condition
Using the wrong message or no message
✗ Incorrect
The first argument is the failure message, then the condition checks if x is greater than y.