Testing / JUnit6 Interview Questions
What are the key differences between JUnit 4 and JUnit 6?
JUnit 4 and JUnit 6 have fundamentally different architectures, annotation models, and extension mechanisms. This is the most common comparison question in interviews where teams have legacy JUnit 4 code.
| Aspect | JUnit 4 | JUnit 6 |
|---|---|---|
| Package | org.junit | org.junit.jupiter.api |
| Test annotation | @Test (org.junit) | @Test (org.junit.jupiter.api) |
| Setup | @Before / @BeforeClass | @BeforeEach / @BeforeAll |
| Teardown | @After / @AfterClass | @AfterEach / @AfterAll |
| Ignore | @Ignore | @Disabled |
| Categorise | @Category | @Tag |
| Expected exception | @Test(expected=Ex.class) | assertThrows() |
| Timeout | @Test(timeout=1000) | assertTimeout(Duration.ofSeconds(1), ...) |
| Extension mechanism | @RunWith + @Rule + @ClassRule | @ExtendWith (single composable model) |
| Runners | @RunWith(MockitoJUnitRunner.class) | @ExtendWith(MockitoExtension.class) |
| Parameterised | @RunWith(Parameterized.class) | @ParameterizedTest (inline, no runner needed) |
| Assert style | Assert.assertEquals() | Assertions.assertEquals() or AssertJ |
| Minimum Java | Java 5 | Java 17 (JUnit 6) |
More Related questions...