Bird
Raised Fist0
JUnittesting~10 mins

Test independence in JUnit - Interactive Code Practice

Choose your learning style10 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

Practice

(1/5)
1.

What does test independence mean in JUnit testing?

Choose the best explanation.

easy
A. Tests must run in a specific order to pass correctly.
B. Tests should always use the same objects to save memory.
C. Tests share data to reduce setup time.
D. Each test runs without depending on the results or state of other tests.

Solution

  1. Step 1: Understand test independence concept

    Test independence means tests do not rely on each other's results or states.
  2. Step 2: Compare options with definition

    Only Each test runs without depending on the results or state of other tests. correctly states that tests run alone without dependencies.
  3. Final Answer:

    Each test runs without depending on the results or state of other tests. -> Option D
  4. Quick Check:

    Test independence = tests run alone [OK]
Hint: Tests should not rely on others to pass [OK]
Common Mistakes:
  • Thinking tests must run in order
  • Sharing data between tests
  • Reusing objects without reset
2.

Which JUnit annotation is best used to prepare fresh data before each test to ensure test independence?

easy
A. @BeforeClass
B. @BeforeEach
C. @AfterAll
D. @Test

Solution

  1. Step 1: Identify setup annotations in JUnit

    @BeforeEach runs before every test method, preparing fresh data each time.
  2. Step 2: Match annotation to test independence need

    @BeforeClass runs once before all tests, not before each, so it doesn't ensure fresh data per test.
  3. Final Answer:

    @BeforeEach -> Option B
  4. Quick Check:

    @BeforeEach prepares fresh data [OK]
Hint: Use @BeforeEach for fresh setup before every test [OK]
Common Mistakes:
  • Using @BeforeClass which runs only once
  • Confusing @AfterAll with setup
  • Not using any setup annotation
3.

Consider this JUnit test class snippet:

class CounterTest {
  int count = 0;

  @Test
  void testIncrement() {
    count++;
    assertEquals(1, count);
  }

  @Test
  void testReset() {
    assertEquals(0, count);
  }
}

What will happen if these tests run in this order without any setup method?

medium
A. testIncrement passes but testReset may fail due to shared count state.
B. testIncrement fails if run after testReset because count is 0.
C. testIncrement may fail if testReset runs first because count is shared.
D. Both tests pass because count is reset manually.

Solution

  1. Step 1: Analyze shared variable usage

    The variable count is shared across tests and not reset before each test, leading to order dependency.
  2. Step 2: Consider test order impact

    If testIncrement runs first: count++ to 1, assert 1 passes (leaves count=1); testReset: assertEquals(0, 1) fails. If testReset first: assert 0 (initial) passes (leaves 0), testIncrement ++ to 1 passes. Thus, testReset may fail due to shared state.
  3. Final Answer:

    testIncrement passes but testReset may fail due to shared count state. -> Option A
  4. Quick Check:

    Shared state causes test order dependency [OK]
Hint: Shared variables cause test order dependency [OK]
Common Mistakes:
  • Assuming tests run isolated without setup
  • Ignoring shared mutable state
  • Thinking manual reset in testReset fixes all issues
4.

Identify the problem in this JUnit test class and how to fix it to ensure test independence:

class ListTest {
  List<String> list = new ArrayList<>();

  @Test
  void testAdd() {
    list.add("item");
    assertEquals(1, list.size());
  }

  @Test
  void testClear() {
    list.clear();
    assertEquals(0, list.size());
  }
}
medium
A. Problem: list is shared and not reset; Fix: initialize list in @BeforeEach method.
B. Problem: list is not final; Fix: make list final to avoid changes.
C. Problem: testClear should run before testAdd; Fix: reorder tests.
D. Problem: list should be static; Fix: declare list as static.

Solution

  1. Step 1: Identify shared mutable state

    The list is a shared instance variable and is not reset before each test, causing tests to affect each other.
  2. Step 2: Apply proper setup for independence

    Initializing the list inside a method annotated with @BeforeEach ensures a fresh list before every test, maintaining independence.
  3. Final Answer:

    Problem: list is shared and not reset; Fix: initialize list in @BeforeEach method. -> Option A
  4. Quick Check:

    Use @BeforeEach to reset shared objects [OK]
Hint: Reset shared objects in @BeforeEach for independence [OK]
Common Mistakes:
  • Making list final does not reset contents
  • Reordering tests does not guarantee independence
  • Using static list shares state across tests
5.

You have a JUnit test class where one test modifies a static shared resource, causing other tests to fail intermittently.

How can you redesign the tests to maintain test independence and avoid flaky failures?

hard
A. Combine all tests into one method to control resource usage.
B. Run tests sequentially to prevent concurrent modification.
C. Avoid using static shared resources; initialize fresh instances in @BeforeEach.
D. Use @AfterAll to reset the static resource once after all tests.

Solution

  1. Step 1: Understand static shared resource issue

    Static resources are shared across all tests and can cause interference if modified without reset.
  2. Step 2: Apply test independence principle

    To ensure independence, avoid static shared mutable state or reset it before each test by creating fresh instances in @BeforeEach.
  3. Step 3: Evaluate other options

    Running tests sequentially or resetting once after all tests does not guarantee independence. Combining tests reduces clarity and independence.
  4. Final Answer:

    Avoid using static shared resources; initialize fresh instances in @BeforeEach. -> Option C
  5. Quick Check:

    Fresh instances per test ensure independence [OK]
Hint: No static shared state; fresh setup each test [OK]
Common Mistakes:
  • Relying on test order or sequence
  • Resetting static resource only once
  • Merging tests reduces clarity and independence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard