0
0
JUnittesting~20 mins

Test independence in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Test Independence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is test independence important in JUnit?

In JUnit testing, why should each test method be independent from others?

ATo reduce the total number of test methods needed
BTo ensure tests can run in any order without affecting results
CTo allow tests to share state for faster execution
DTo make tests dependent on the success of previous tests
Attempts:
2 left
💡 Hint

Think about what happens if tests rely on each other's data or state.

Predict Output
intermediate
2:00remaining
Output of dependent JUnit tests

Consider these two JUnit test methods. What will be the output if testB runs before testA?

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

public class SampleTest {
    private static int counter = 0;

    @Test
    public void testA() {
        counter = 5;
        assertEquals(5, counter);
    }

    @Test
    public void testB() {
        assertEquals(5, counter);
    }
}
ABoth testA and testB pass
BBoth testA and testB fail
CtestA fails because counter is not set, testB passes
DtestB fails because counter is 0, testA passes
Attempts:
2 left
💡 Hint

Remember static variables keep their value across tests unless reset.

assertion
advanced
2:00remaining
Correct assertion for independent test

Which assertion best ensures that a JUnit test is independent and does not rely on previous test state?

AassertTrue(sharedFlag) set by another test
BassertNotNull(sharedObject) without initialization in this test
CassertEquals(expectedValue, actualValue) with setup in @BeforeEach
DassertEquals(0, counter) relying on static variable from previous test
Attempts:
2 left
💡 Hint

Think about where the test data comes from and if it is initialized fresh each time.

🔧 Debug
advanced
2:00remaining
Identify the cause of flaky JUnit tests

These JUnit tests sometimes fail and sometimes pass. What is the most likely cause?

JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;

public class FlakyTest {
    private static List<String> sharedList = new ArrayList<>();

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

    @Test
    public void testClear() {
        sharedList.clear();
        assertEquals(0, sharedList.size());
    }
}
ATests share mutable static state causing order-dependent failures
BTests use different assertion methods causing conflicts
CTests have syntax errors causing random failures
DTests run in parallel causing thread safety issues
Attempts:
2 left
💡 Hint

Look at how the sharedList is used and modified.

framework
expert
2:00remaining
Best practice to ensure test independence in JUnit 5

Which JUnit 5 feature helps guarantee test independence by resetting state before each test method?

A@BeforeEach annotated method to initialize fresh state
B@TestMethodOrder to run tests in a specific sequence
C@BeforeAll to set up shared state once for all tests
D@RepeatedTest to run the same test multiple times
Attempts:
2 left
💡 Hint

Think about how to prepare the environment freshly for each test.