TL;DR: Most JUnit interviews in 2026 test five areas: the JUnit 5 and 6 architecture (Platform, Jupiter, Vintage), annotations and the test lifecycle, assertions and assumptions, parameterized and dynamic tests, and how JUnit works with Mockito and CI. This guide answers the questions asked most often, from beginner to advanced, with the facts checked against the official JUnit documentation.
JUnit is the standard unit testing framework for Java. The current major version is JUnit 6, released on September 30, 2025, which requires Java 17 or later (JUnit release notes). Most of what interviewers ask about JUnit 5 still applies to JUnit 6, because the Jupiter programming model is the same. Where they differ, the answer below says so.
For a hands-on walkthrough, read our guide on how to write JUnit test cases.
Beginner JUnit interview questions
1. What is JUnit?
JUnit is an open source framework for writing and running repeatable unit tests in Java. You write test methods, mark them with annotations such as @Test, and use assertions to compare expected and actual results. Build tools such as Maven and Gradle and every major Java IDE can run JUnit tests.
2. What are JUnit Platform, JUnit Jupiter and JUnit Vintage?
They are the three parts of modern JUnit (JUnit user guide):
- JUnit Platform is the foundation for launching testing frameworks on the JVM. It defines the TestEngine API and provides a console launcher.
- JUnit Jupiter is the programming model and extension model for writing tests, plus the engine that runs them.
- JUnit Vintage is an engine for running old JUnit 3 and JUnit 4 tests. It is deprecated and meant only for temporary use during migration.
3. What is the difference between JUnit 4 and JUnit 5?
JUnit 4 was a single library. JUnit 5 split it into Platform, Jupiter and Vintage, added an extension model, and renamed several annotations:
| JUnit 4 | JUnit 5 and 6 | Purpose |
|---|---|---|
| @Before | @BeforeEach | Runs before each test method |
| @After | @AfterEach | Runs after each test method |
| @BeforeClass | @BeforeAll | Runs once before all tests in the class |
| @AfterClass | @AfterAll | Runs once after all tests in the class |
| @Ignore | @Disabled | Skips a test class or method |
| @Category | @Tag | Groups tests for filtering |
| @RunWith | @ExtendWith | Plugs in extra behavior |
| @Test(expected = X.class) | assertThrows | Checks that an exception is thrown |
4. What changed in JUnit 6?
JUnit 6.0.0 was released on September 30, 2025. It requires Java 17, gives Platform, Jupiter and Vintage a single shared version number, expresses nullability with JSpecify annotations, supports Kotlin suspend functions as test methods, and removes several deprecated APIs (release notes). The Vintage engine is deprecated.
5. Do JUnit test classes and methods have to be public?
No. In JUnit 5 and later, test classes, test methods and lifecycle methods do not need to be public, but they must not be private. The JUnit team recommends leaving out public unless there is a technical reason for it.
6. What is the difference between @BeforeEach and @BeforeAll?
@BeforeEach runs before every test method, so it is used for fresh setup such as creating a new object. @BeforeAll runs once before all tests in the class, for expensive shared setup such as starting a test database. By default, @BeforeAll and @AfterAll methods must be static.
7. What does @DisplayName do?
It sets a readable name for a test class or method in reports and IDEs, for example "returns 404 when the user does not exist" instead of a method name.
8. How do you skip a test in JUnit?
Mark it with @Disabled, optionally with a reason. For conditional skipping, use assumptions or conditional annotations, which are covered below.
Intermediate JUnit interview questions
9. What is the test instance lifecycle in JUnit?
By default, JUnit creates a new instance of the test class before running each test method, which is called the per-method lifecycle. This keeps tests independent because no instance fields carry over. If you annotate the class with @TestInstance(Lifecycle.PER_CLASS), JUnit uses one instance for all methods, and @BeforeAll and @AfterAll no longer need to be static.
10. What is the difference between assertAll and separate assertions?
With separate assertions, the test stops at the first failure, so you only see one problem per run. assertAll groups several assertions, runs all of them, and reports every failure together. It is useful when checking several fields of one object.
11. How do you test that a method throws an exception?
Use assertThrows with the expected exception type and the code to run. It passes if that exception or a subclass is thrown, and it returns the exception so you can also check its message.
12. What is the difference between assertTimeout and assertTimeoutPreemptively?
Both fail a test that runs longer than a given duration. assertTimeout runs the code in the same thread and fails after it finishes. assertTimeoutPreemptively runs the code in a different thread and aborts it when the time runs out. Because it uses another thread, ThreadLocal state may not carry over, which the JUnit docs warn can cause side effects, for example with transactions.
13. What are assumptions in JUnit?
Assumptions such as assumeTrue check a precondition before the test logic runs. If the assumption is false, JUnit aborts the test instead of failing it, so it is reported as skipped. A common use is running a test only on a CI server or a specific operating system.
14. What are parameterized tests?
A parameterized test, marked with @ParameterizedTest, runs the same test method several times with different inputs. The inputs come from a source annotation such as @ValueSource for simple values, @CsvSource or @CsvFileSource for rows of data, @EnumSource for enum constants, and @MethodSource or @FieldSource for values built in code.
15. What is the difference between @RepeatedTest and @ParameterizedTest?
@RepeatedTest runs the same test with the same input a fixed number of times, which helps find flaky behavior. @ParameterizedTest runs the test once per input value, which helps cover many cases with one method.
16. What are tags used for?
@Tag labels tests, for example "fast", "slow" or "integration". Build tools and the JUnit Platform can then include or exclude tests by tag, so a pull request can run only fast tests while a nightly build runs everything.
17. What is a nested test class?
@Nested marks a non-static inner class as a group of related tests. Nested classes can have their own setup methods, which makes it easy to describe behavior in context, such as "when the cart is empty" and "when the cart has items".

Advanced JUnit interview questions
18. What are dynamic tests?
Dynamic tests are created at runtime by a method marked @TestFactory, which returns a stream or collection of DynamicTest objects. Unlike parameterized tests, the number and names of tests can depend on data read while the test runs.
19. How does the JUnit extension model work?
Extensions add behavior around tests, such as dependency injection, setup, or custom reporting. You register them declaratively with @ExtendWith or programmatically with @RegisterExtension. For example, Mockito's MockitoExtension and Spring's SpringExtension both plug in this way.
20. How do you use JUnit with Mockito?
Annotate the test class with @ExtendWith(MockitoExtension.class), create mocks for dependencies with @Mock, and inject them into the class under test with @InjectMocks. You then stub calls with when(...).thenReturn(...) and check interactions with verify(...). Our guide to writing JUnit test cases shows a full example.
21. How do you use a temporary directory in a test?
Annotate a field or parameter of type Path or File with @TempDir. JUnit creates the directory before the test and deletes it afterwards, so file based tests stay clean and independent.
22. Can JUnit run tests in parallel?
Yes, but it is opt in. You enable it with the configuration property junit.jupiter.execution.parallel.enabled set to true, usually in junit-platform.properties, and then choose which classes or methods run concurrently. Tests that share state, such as static fields or one database, need care or should stay sequential.
23. How do you run JUnit tests in CI?
Run them through the build tool: mvn test for Maven or ./gradlew test for Gradle. Both find all test methods, return a non-zero exit code when a test fails, and write reports that CI systems such as Jenkins or GitHub Actions can display. Use tags to split fast and slow suites across pipeline stages.
24. How do you migrate a JUnit 4 suite to JUnit 5 or 6?
Add the Jupiter dependencies, keep old tests running temporarily on the Vintage engine, and convert classes step by step: change imports to org.junit.jupiter.api, rename lifecycle annotations, replace @RunWith with @ExtendWith, and replace expected exceptions with assertThrows. Because Vintage is deprecated, plan to finish the migration rather than keep both models long term.
25. What does JUnit not catch?
JUnit proves that individual units of Java code behave as expected. It does not prove that a real user can complete a journey across the browser, API and backend, and mocks can hide integration problems. Interviewers often ask this to see whether you understand the testing pyramid: many fast unit tests, fewer integration tests, and a small set of end to end tests.
How to prepare for a JUnit interview
- Write a small project with a service class, a repository mock and 10 to 15 tests that use the annotations above.
- Practice explaining the per-method lifecycle and why it keeps tests independent.
- Convert one JUnit 4 test class to JUnit 5 or 6 so you can talk about migration from experience.
- Run your tests from the command line and in a CI pipeline, not only in the IDE.
- Be ready to explain where unit tests stop and end to end tests begin.
Where end to end testing fits
Strong JUnit skills cover the unit layer. Most teams also need end to end tests that check real user journeys, and those are slower to write and maintain. ContextQA handles that layer: you describe the journey in plain English, and the platform generates the steps, runs them in real browsers and captures evidence (introduction). Tests can be exported as a Playwright project if you want them in code (export and portability). For more on modern unit testing, see AI unit testing and TDD vs BDD.
Bottom line
JUnit interviews reward clear understanding over memorized lists. Know the Platform, Jupiter and Vintage split, the main annotations and lifecycle, the difference between assertions and assumptions, and how parameterized tests, extensions and Mockito fit together. Then show that you know where unit tests stop. For a broader Java testing overview, read our JUnit testing guide.