Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to run the setup method before each test.
JUnit
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class CalculatorTest { private Calculator calculator; @[1] void setup() { calculator = new Calculator(); } @Test void testAddition() { int result = calculator.add(2, 3); assertEquals(5, result); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll instead of @BeforeEach
Forgetting the @BeforeEach annotation
Using @Test annotation on setup method
✗ Incorrect
The @BeforeEach annotation tells JUnit to run the setup method before each test method.
2fill in blank
mediumComplete the code to initialize the list before each test.
JUnit
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; public class ListTest { private List<String> items; @BeforeEach void init() { items = new [1]<>(); } @Test void testListIsEmpty() { assertTrue(items.isEmpty()); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Set implementations instead of List
Not initializing the list before tests
Using LinkedList which is less common here
✗ Incorrect
ArrayList is a common List implementation used to initialize the list before each test.
3fill in blank
hardFix the error in the setup method annotation.
JUnit
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class UserServiceTest { private UserService service; @[1] void setup() { service = new UserService(); } @Test void testUserCreation() { assertNotNull(service); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong casing like @Beforeall
Adding parentheses to annotation
Using non-existent annotations
✗ Incorrect
The correct annotation is @BeforeEach without parentheses and with correct casing.
4fill in blank
hardFill both blanks to create a setup method that initializes count and flag before each test.
JUnit
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class CounterTest { private int count; private boolean flag; @[1] void setup() { count = 0; flag = [2]; } @Test void testInitialValues() { assertEquals(0, count); assertFalse(flag); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll instead of @BeforeEach
Setting flag to true instead of false
Forgetting the annotation
✗ Incorrect
The setup method must be annotated with @BeforeEach and flag initialized to false before each test.
5fill in blank
hardFill all three blanks to create a setup method that initializes a map with a key and value before each test.
JUnit
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; import static org.junit.jupiter.api.Assertions.*; public class MapTest { private Map<String, Integer> data; @[1] void setup() { data = new [2]<>(); data.put("count", [3]); } @Test void testMapInitialization() { assertEquals(1, data.get("count")); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll instead of @BeforeEach
Using TreeMap instead of HashMap
Putting wrong value in the map
✗ Incorrect
The setup method uses @BeforeEach, initializes data as a HashMap, and puts key "count" with value 1.