0
0
JUnittesting~10 mins

Test independence 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 mark the test method as independent in JUnit 5.

JUnit
import org.junit.jupiter.api.Test;

public class CalculatorTest {

    @[1]
    void testAddition() {
        // test code here
    }
}
Drag options to blanks, or click blank then click option'
ABeforeEach
BDisabled
CAfterEach
DTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach or @AfterEach instead of @Test
Forgetting to add any annotation
2fill in blank
medium

Complete the code to ensure each test method runs with a fresh Calculator instance.

JUnit
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CalculatorTest {
    Calculator calculator;

    @BeforeEach
    void [1]() {
        calculator = new Calculator();
    }

    @Test
    void testAddition() {
        // test code here
    }
}
Drag options to blanks, or click blank then click option'
Asetup
Binitialize
CbeforeAll
Dreset
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll instead of @BeforeEach
Naming the method something unrelated
3fill in blank
hard

Fix the error in the test method to ensure test independence by avoiding shared state.

JUnit
import org.junit.jupiter.api.Test;

public class CounterTest {
    static int count = 0;

    @Test
    void testIncrement() {
        count++;
        assertEquals(1, [1]);
    }
}
Drag options to blanks, or click blank then click option'
ACounterTest.count
Bthis.count
Ccount
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance variable syntax for static variable
Asserting wrong expected value
4fill in blank
hard

Fill both blanks to create independent tests by resetting shared state before each test.

JUnit
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class SharedResourceTest {
    static int resourceCount = 0;

    @BeforeEach
    void [1]() {
        resourceCount = [2];
    }

    @Test
    void testOne() {
        resourceCount++;
        assertEquals(1, resourceCount);
    }

    @Test
    void testTwo() {
        resourceCount += 2;
        assertEquals(2, resourceCount);
    }
}
Drag options to blanks, or click blank then click option'
Areset
Binitialize
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Not resetting the static variable
Setting the variable to 1 instead of 0
5fill in blank
hard

Fill all three blanks to write independent tests using instance variables and proper setup.

JUnit
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BankAccountTest {
    BankAccount [1];

    @BeforeEach
    void [2]() {
        [1] = new BankAccount();
    }

    @Test
    void testDeposit() {
        [1].deposit(100);
        assertEquals(100, [1].getBalance());
    }
}
Drag options to blanks, or click blank then click option'
Aaccount
Bsetup
Cbalance
Dinitialize
Attempts:
3 left
💡 Hint
Common Mistakes
Using static variables instead of instance variables
Not initializing the object before tests