Recall & Review
beginner
What does test independence mean in software testing?
Test independence means each test runs alone without relying on other tests. Tests should not share data or depend on the order of execution.
Click to reveal answer
beginner
Why is test independence important?
It helps find bugs clearly and makes tests reliable. If tests depend on each other, one failure can cause many false failures.
Click to reveal answer
intermediate
How can you ensure test independence in JUnit?
Use fresh objects for each test, avoid shared static data, and reset states in @BeforeEach methods.
Click to reveal answer
beginner
What is a common problem if tests are not independent?
Tests may pass or fail depending on the order they run. This causes flaky tests that are hard to trust.
Click to reveal answer
intermediate
Show a simple JUnit example that demonstrates test independence.
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CounterTest {
private Counter counter;
@BeforeEach
void setup() {
counter = new Counter(); // fresh object for each test
}
@Test
void testIncrement() {
counter.increment();
assertEquals(1, counter.getValue());
}
@Test
void testReset() {
counter.increment();
counter.reset();
assertEquals(0, counter.getValue());
}
}
Each test uses a new Counter object, so tests do not affect each other.Click to reveal answer
What does test independence ensure?
✗ Incorrect
Test independence means tests do not rely on each other and can run in any order with consistent results.
Which JUnit annotation helps prepare a fresh state before each test?
✗ Incorrect
@BeforeEach runs before every test method, allowing setup of fresh objects to keep tests independent.
What problem arises if tests share static variables?
✗ Incorrect
Sharing static variables can cause tests to affect each other, leading to unpredictable failures.
If test A must run before test B for B to pass, what is violated?
✗ Incorrect
Test independence requires tests to run successfully regardless of order.
Which practice helps maintain test independence?
✗ Incorrect
Resetting test data before each test ensures tests do not affect each other.
Explain what test independence means and why it is important in JUnit testing.
Think about how tests should not rely on each other.
You got /3 concepts.
Describe how you would write JUnit tests to ensure they are independent.
Consider setup methods and object creation.
You got /3 concepts.