JUnit Java Testing Interview
Curated 20+ questions covering JUnit 5 Jupiter architecture, common annotations, assertions, nested suites, and parallel configurations.
Difficulty Filters
Tools & Options
What is JUnit, and what are the main differences between JUnit 4 and JUnit 5?
JUnit is a popular unit testing framework for the Java programming language. JUnit 5 is a modular rewrite consisting of three sub-modules: JUnit Platform (runner engine), JUnit Jupiter (new annotations and extension API), and JUnit Vintage (supports backward compatibility for JUnit 3 and 4).
List common JUnit 5 annotations and their JUnit 4 equivalents.
1. `@Test`: Defines a test method (same in both).
2. `@BeforeEach` (JUnit 5) vs `@Before` (JUnit 4): Runs before every test method.
3. `@AfterEach` (JUnit 5) vs `@After` (JUnit 4): Runs after every test method.
4. `@BeforeAll` (JUnit 5) vs `@BeforeClass` (JUnit 4): Runs once before all tests (must be static).
5. `@AfterAll` (JUnit 5) vs `@AfterClass` (JUnit 4): Runs once after all tests (must be static).
6. `@Disabled` (JUnit 5) vs `@Ignore` (JUnit 4): Disables/skips the test.
How do you test that a method throws an exception in JUnit 5?
In JUnit 5, you assert exceptions using the `Assertions.assertThrows()` method. This accepts the expected Exception class and an executable lambda block. It returns the thrown exception instance, allowing you to assert detail messages.
@Test
void testException() {
Exception exception = assertThrows(ArithmeticException.class, () -> {
int result = 1 / 0;
});
assertEquals("/ by zero", exception.getMessage());
}Explain Parameterized Tests in JUnit 5.
What are assertions in JUnit? List some common ones.
Explain the difference between `assertTimeout()` and `assertTimeoutPreemptively()`.
What are Assumptions in JUnit 5, and how do they differ from Assertions?
What is `@DisplayName` annotation?
What is the Extension Model in JUnit 5, and what annotation replaces `@RunWith`?
Explain Grouped Assertions (`assertAll()`).
What is the default lifecycle of a test class instance in JUnit?
Explain `@Nested` tests in JUnit 5.
What is the purpose of `@Tag` in JUnit 5?
How do you run parameterized tests with CSV data files?
What is `@TempDir` in JUnit 5, and why is it used?
What is a dynamic test in JUnit 5?
How do you configure parallel test execution in JUnit 5?
What is `@RepeatedTest` in JUnit 5?
Explain the use of the `@Mock` and `@InjectMocks` annotations in Mockito JUnit tests.
How can you order the execution of test methods in JUnit 5?
Technical Interview Mastery Blueprint
Proven methodologies and frameworks to succeed in technical and architecture rounds.
1. The Engineering STAR Technique
When responding to architectural or situational questions, structure your answer using Situation, Technical Constraint, Action, and Measurable Result. Rather than jumping straight to syntax, articulate the business problem, why certain patterns (e.g. caching, queuing, immutability) were selected over alternatives, and what latency or throughput improvements were achieved.
2. Defending Architectural Trade-Offs
Senior interviewers evaluate how you weigh trade-offs. Always explain why you chose an approach: e.g., memory overhead vs CPU cycle speed, consistency vs availability (CAP theorem), or normalized schemas vs denormalized fast reads. Stating the drawbacks of your own proposal demonstrates maturity and production experience.
3. Clean Code & Edge-Case Proactivity
In live-coding rounds, communicate your thinking aloud before typing. State edge cases explicitly: null or undefined inputs, extreme boundary values, concurrency races, and network timeouts. Writing modular, well-named functions with deterministic unit tests separates junior candidates from staff-level engineers.
4. Antipatterns to Avoid
Avoid reciting memorized definitions without practical context. Never dismiss non-functional requirements such as security (CSRF, injection, token rotation), observability (logging, metrics), and accessibility (ARIA, semantic standards). Always ask clarifying scope questions before writing complex implementations.
Explore Related Tools
Hand-picked utilities and calculators related to this tool.
