JUnit 5
41 questions in 14 subtopics.
Test lifecycle
In JUnit 5, how many instances of a test class are created by default, and what changes with@TestInstance(Lifecycle.PER_CLASS)?In what order do these run for a class with two test methods: @BeforeAll, @BeforeEach, @Test, @AfterEach, @AfterAll? What happens to @AfterEach if a test throws?What changed for teams moving from JUnit 4 to JUnit 5, and what does JUnit 6 change compared with JUnit 5?Your codebase has tens of thousands of JUnit 4 tests. How would you plan and justify migrating to JUnit 5 or 6?Assertions
What doesassertAll do, and when is it better than several separate assertions?What is wrong with this test, and how would you write it with assertThrows?try {
service.withdraw(-5);
fail();
} catch (IllegalArgumentException e) { }
assertTimeoutPreemptively around a call to a @Transactional Spring service and the database changes are not rolled back as expected. Why?Parameterized tests
Write a JUnit 5 parameterized test that checks anisValidEmail method with three valid and three invalid inputs, using @CsvSource.When would you use @MethodSource or a custom ArgumentsProvider instead of @CsvSource, and what are the risks of large parameterized tests?Several teams test the same validation rules (currency codes, IBANs, country formats) with copied parameterized tests. How would you share test cases across services?Extensions
How does the JUnit 5 extension model replace JUnit 4 runners and rules? Give two built-in examples used in Spring projects.Write a JUnit 5 extension that measures the time of each test method and logs tests slower than 500 ms.Your organisation's tests repeat the same setup for clocks, test users and database cleanup across 50 services. Design a shared test library using JUnit extensions.Your shared test library of JUnit extensions is used by 200 services and has become hard to change because teams depend on its behaviour. How would you manage it over the long term?Nested and dynamic tests
What are@Nested tests in JUnit 5 and when do they improve readability?How do dynamic tests created by a @TestFactory differ from regular tests in terms of lifecycle callbacks?Test ordering and isolation
A test class passes when run alone but fails when run with the whole suite. What are common causes in a Spring Boot project?How do you enable parallel test execution in JUnit 5, and how do you protect tests that share a resource?Your teams frequently use@TestMethodOrder to make tests depend on each other's results. What standard would you set and why?Your company's total CI test time is large enough to be a major cloud cost. Leadership asks for a 40 percent cut without reducing confidence. Which JUnit-level and suite-level strategies would you propose?Assumptions and conditional execution
What is the difference between an assertion failure and an assumption failure in JUnit 5? ShowAssumptions.assumeTrue and assumingThat, and two annotations that enable a test only in some environments.A suite has 400 tests guarded by assumeTrue(dockerAvailable()). CI reports green builds, but a database migration bug reached production because those tests were silently skipped on the new CI runners. How do you prevent this?Across your organisation, thousands of tests are disabled with @Disabled, conditional annotations or assumptions, many for years. What policy and tooling would you introduce?Tags and test suites
Tag slow integration tests with@Tag("integration") and show how to run only fast tests locally and all tests in CI with Maven Surefire and with Gradle. How do tag expressions work?Explain how to define a test suite in JUnit 5 with the junit-platform-suite engine, and when a suite class is better than build-tool tag filters.Your monorepo has 30,000 tests and a 50-minute pipeline. Design a tagging and test-selection strategy across unit, integration, contract and end-to-end tests that keeps feedback fast without losing coverage.Test timeouts
Show two ways to fail a JUnit 5 test that takes longer than 2 seconds, explain the difference between@Timeout and assertTimeout, and how to set a default timeout for all tests.An integration test hangs forever in CI about once a week, blocking the pipeline for hours until the job is killed, and the logs show nothing. How do you make it fail fast and find the cause?Repeated tests
Write a@RepeatedTest that runs a concurrency check 50 times, includes the repetition number in the display name and stops after 3 failures. What are the limits of this approach for flaky tests?Your team wants to add automatic retries for failing tests in CI (for example Surefire rerunFailingTestsCount or a retry extension). What are the risks and what would you do instead or in addition?Launcher and execution listeners
What is the JUnit Platform Launcher, and how do IDEs and build tools use it? ExplainTestEngine, the Launcher API and TestExecutionListener.Write a TestExecutionListener that records the duration of every test and writes the 20 slowest to a file at the end of the run, registered for all builds without changing test classes.You want organisation-wide test analytics (flaky rate, slowest tests, failure clustering) across 300 repositories using JUnit. How would you collect the data?Build setup and reports
Set up JUnit 5 in a Maven project with the BOM, Surefire for unit tests and Failsafe for integration tests named *IT. What happens if you add JUnit Jupiter but use a very old Surefire version?After a dependency upgrade, mvn test reports 'Tests run: 0' for a module that has 300 JUnit 5 tests, and the build passes. List the likely causes and how you would make this impossible to miss.Over 15 years your company accumulated Ant, Maven and Gradle builds with different test setups and report formats. What would you standardise, and how do you justify the cost?JUnit versions and migration
How can JUnit 4 and JUnit 5 tests run in the same project during a migration, and which JUnit 4 features have no direct one-to-one replacement?You plan to move a large Spring Boot codebase from JUnit 5 to JUnit 6. What do you check first, and what could break?Several teams want to stay on JUnit 4 because 'it works'. As the engineering lead for 80 services, how do you decide whether to force migration?Assertion libraries for JSON and XML
Write a test that checks a JSON response body from a REST call matches an expected document, ignoring field order and an extra timestamp field. Which libraries can you use?A SOAP integration returns XML with namespaces, a generated id and elements in varying order. Write an XMLUnit-based assertion that ignores whitespace, the id element and order of sibling items.No question matches these choices.