0
0
JUnittesting~10 mins

@BeforeEach method 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 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'
AAfterEach
BBeforeAll
CTest
DBeforeEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll instead of @BeforeEach
Forgetting the @BeforeEach annotation
Using @Test annotation on setup method
2fill in blank
medium

Complete 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'
AArrayList
BTreeSet
CHashSet
DLinkedList
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
3fill in blank
hard

Fix 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'
ABeforeEach
BBeforeall
CBeforeEach()
DBeforeEachTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong casing like @Beforeall
Adding parentheses to annotation
Using non-existent annotations
4fill in blank
hard

Fill 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'
ABeforeEach
Btrue
Cfalse
DBeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll instead of @BeforeEach
Setting flag to true instead of false
Forgetting the annotation
5fill in blank
hard

Fill 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'
ABeforeEach
BHashMap
C1
DTreeMap
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll instead of @BeforeEach
Using TreeMap instead of HashMap
Putting wrong value in the map