Scrivio Interview library

JUnit 5

41 questions in 14 subtopics.

Test lifecycle

ConceptFoundations6 min read
In JUnit 5, how many instances of a test class are created by default, and what changes with @TestInstance(Lifecycle.PER_CLASS)?
CodingFoundations5 min read
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?
ConceptIntermediate6 min read
What changed for teams moving from JUnit 4 to JUnit 5, and what does JUnit 6 change compared with JUnit 5?
LeadershipPrincipal6 min read
Your codebase has tens of thousands of JUnit 4 tests. How would you plan and justify migrating to JUnit 5 or 6?

Assertions

ConceptFoundations5 min read
What does assertAll do, and when is it better than several separate assertions?
CodingFoundations5 min read
What is wrong with this test, and how would you write it with assertThrows?
try {
service.withdraw(-5);
fail();
} catch (IllegalArgumentException e) { }
DebuggingIntermediate6 min read
A test uses assertTimeoutPreemptively around a call to a @Transactional Spring service and the database changes are not rolled back as expected. Why?

Parameterized tests

CodingFoundations5 min read
Write a JUnit 5 parameterized test that checks an isValidEmail method with three valid and three invalid inputs, using @CsvSource.
ConceptIntermediate6 min read
When would you use @MethodSource or a custom ArgumentsProvider instead of @CsvSource, and what are the risks of large parameterized tests?
DesignAdvanced5 min read
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

ConceptFoundations5 min read
How does the JUnit 5 extension model replace JUnit 4 runners and rules? Give two built-in examples used in Spring projects.
CodingIntermediate5 min read
Write a JUnit 5 extension that measures the time of each test method and logs tests slower than 500 ms.
DesignAdvanced6 min read
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.
LeadershipPrincipal6 min read
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

ConceptFoundations5 min read
What are @Nested tests in JUnit 5 and when do they improve readability?
InternalsIntermediate5 min read
How do dynamic tests created by a @TestFactory differ from regular tests in terms of lifecycle callbacks?

Test ordering and isolation

DebuggingFoundations6 min read
A test class passes when run alone but fails when run with the whole suite. What are common causes in a Spring Boot project?
ConceptIntermediate6 min read
How do you enable parallel test execution in JUnit 5, and how do you protect tests that share a resource?
LeadershipAdvanced6 min read
Your teams frequently use @TestMethodOrder to make tests depend on each other's results. What standard would you set and why?
ScenarioPrincipal6 min read
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

ConceptFoundations6 min read
What is the difference between an assertion failure and an assumption failure in JUnit 5? Show Assumptions.assumeTrue and assumingThat, and two annotations that enable a test only in some environments.
ScenarioIntermediate7 min read
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?
LeadershipAdvanced7 min read
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

CodingFoundations6 min read
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?
DesignIntermediate6 min read
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.
DesignAdvanced7 min read
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

CodingFoundations6 min read
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.
DebuggingIntermediate7 min read
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

CodingFoundations6 min read
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?
ScenarioIntermediate7 min read
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

ConceptFoundations5 min read
What is the JUnit Platform Launcher, and how do IDEs and build tools use it? Explain TestEngine, the Launcher API and TestExecutionListener.
CodingIntermediate6 min read
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.
DesignAdvanced7 min read
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

CodingFoundations6 min read
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?
DebuggingIntermediate7 min read
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.
LeadershipPrincipal6 min read
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

ConceptFoundations6 min read
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?
ScenarioIntermediate6 min read
You plan to move a large Spring Boot codebase from JUnit 5 to JUnit 6. What do you check first, and what could break?
LeadershipAdvanced6 min read
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

CodingFoundations5 min read
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?
CodingIntermediate6 min read
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.