Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the JUnit test annotation.
JUnit
import org.junit.[1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @Before or @After instead of @Test
Forgetting to import any annotation
✗ Incorrect
The @Test annotation marks a method as a test method in JUnit.
2fill in blank
mediumComplete the code to create a test method named testAddition.
JUnit
@Test
public void [1]() {
// test code here
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that don't start with 'test'
Using invalid Java method names
✗ Incorrect
By convention, test methods often start with 'test' followed by the feature name, like testAddition.
3fill in blank
hardFix the error in the assertion to check if sum equals 5.
JUnit
int sum = 2 + 3; assertEquals([1], sum);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the actual value as expected
Using a string instead of an integer
✗ Incorrect
The expected value should be the integer 5, not the variable sum or a string.
4fill in blank
hardFill both blanks to import the assertion and write a failing test method.
JUnit
import static org.junit.Assert.[1]; @Test public void testFail() { [2]("Failing test"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing assertTrue but calling fail
Calling assertEquals instead of fail
✗ Incorrect
The fail method from JUnit causes the test to fail immediately with a message.
5fill in blank
hardFill all three blanks to write a test that checks if a list contains a value.
JUnit
import static org.junit.Assert.[1]; import java.util.List; @Test public void testListContains() { List<String> items = List.of("apple", "banana", "cherry"); boolean contains = items.contains("banana"); [2]("List should contain banana", contains); [3](true, contains); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertFalse instead of assertTrue
Mixing up assertTrue and assertEquals
✗ Incorrect
Use assertTrue to check the condition is true, and assertEquals to compare expected and actual values.