Back to Tech Interviews

JUnit Java Testing Interview

Curated 20+ questions covering JUnit 5 Jupiter architecture, common annotations, assertions, nested suites, and parallel configurations.

Total: 20 QuestionsLearned: 0 / 20 (0%)
Difficulty Filters

Tools & Options
Showing 20 of 20 questions
Beginner
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).

Beginner
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.

Intermediate
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());
}
Intermediate
Explain Parameterized Tests in JUnit 5.
Beginner
What are assertions in JUnit? List some common ones.
Advanced
Explain the difference between `assertTimeout()` and `assertTimeoutPreemptively()`.
Intermediate
What are Assumptions in JUnit 5, and how do they differ from Assertions?
Beginner
What is `@DisplayName` annotation?
Advanced
What is the Extension Model in JUnit 5, and what annotation replaces `@RunWith`?
Intermediate
Explain Grouped Assertions (`assertAll()`).
Intermediate
What is the default lifecycle of a test class instance in JUnit?
Intermediate
Explain `@Nested` tests in JUnit 5.
Beginner
What is the purpose of `@Tag` in JUnit 5?
Intermediate
How do you run parameterized tests with CSV data files?
Advanced
What is `@TempDir` in JUnit 5, and why is it used?
Advanced
What is a dynamic test in JUnit 5?
Advanced
How do you configure parallel test execution in JUnit 5?
Beginner
What is `@RepeatedTest` in JUnit 5?
Scenario
Explain the use of the `@Mock` and `@InjectMocks` annotations in Mockito JUnit tests.
Intermediate
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.

Continuous learning: Practice daily, mark completed questions as learned, and revisit difficult concepts.Explore All Interview Tracks →

Explore Related Tools

Hand-picked utilities and calculators related to this tool.

Related Tools