Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

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.

I would build a small shared test library made of independent JUnit extensions, one for a fixed Clock, one for test users and one for database cleanup, each injecting its fixture through a ParameterResolver. On top I would offer a few composed annotations such as @ServiceTest that combine @ExtendWith, the usual Spring test annotations and sensible defaults. The library is versioned, documented and tested against sample projects, and it deliberately avoids hiding so much behaviour that a failing test becomes hard to understand.

Why this question is asked

When 50 services each copy the same 80 lines of setup for clocks, users and cleanup, every copy is slightly different, and a fix in one never reaches the others. A shared library solves that, but a badly designed one creates a new problem: magic that nobody understands when a test fails. Interviewers ask this design question to see whether we know the JUnit extension model well enough to build reusable pieces, and whether we can judge how much to hide. In this answer, we will split the repeated setup into small extensions, inject fixtures with ParameterResolver, combine them into composed annotations, and look at versioning, testing the library, and the limits of hiding behaviour.

1Looking at the Repeated Setup

Before writing code, we collect what the services actually repeat. Typically three things appear. A fixed clock: tests need a predictable now, so each service builds a Clock.fixed(...) bean or field. Test users: tests need an authenticated user with certain roles, built by hand each time. Database cleanup: integration tests truncate tables between tests, each service with its own list of table names, often out of date.

Each of these is a separate concern, and not every service needs all three. That observation drives the design: three small pieces, not one big base class.

2Small Independent Extensions

Each concern becomes one extension in the library. An extension, a class implementing JUnit callback interfaces, can run code before and after each test and can supply parameters. Keeping them independent lets a service that has no database use the clock and users without dragging in cleanup.

Java
public class FixedClockExtension implements ParameterResolver {
public static final Instant DEFAULT_NOW = Instant.parse("2026-01-15T10:00:00Z");
@Override
public boolean supportsParameter(ParameterContext p, ExtensionContext c) {
return p.getParameter().getType() == Clock.class;
}
@Override
public Object resolveParameter(ParameterContext p, ExtensionContext c) {
return Clock.fixed(DEFAULT_NOW, ZoneOffset.UTC);
}
}
@ExtendWith(FixedClockExtension.class)
class InvoiceTest {
@Test void dueDateIsThirtyDaysLater(Clock clock) { // resolved by the extension
Invoice invoice = new Invoice(clock);
assertEquals(LocalDate.of(2026, 2, 14), invoice.dueDate()); // passes
}
}

Notice that the test asks for a Clock as a parameter and gets one. ParameterResolver is the hook that makes this possible: JUnit asks every registered resolver whether it supports a parameter, and the one that says yes provides the value. This is much clearer than a base class field, because the test declares exactly what it uses. The user extension works the same way, resolving a TestUser parameter, possibly configured with an annotation such as @WithRoles("ADMIN"). The cleanup extension implements AfterEachCallback and truncates the tables that the test's datasource reports, instead of a hand-maintained list.

3Composed Annotations for the Common Cases

Most services will want the same combination, so we offer a composed annotation, an annotation that carries other annotations. JUnit and Spring both read meta-annotations, so putting @ServiceTest on a class is the same as putting all its annotations there.

Java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@SpringBootTest
@ActiveProfiles("test")
@ExtendWith({FixedClockExtension.class, TestUserExtension.class, DatabaseCleanupExtension.class})
@Tag("integration")
public @interface ServiceTest { }
@ServiceTest
class OrderApiTest {
@Test void createsOrder(Clock clock, TestUser user) { /* ... */ }
}
Step throughShape of the shared test library
  1. Composed annotations@ServiceTest, @DomainTest: a few common combinations
  2. Independent extensionsclock, test users, database cleanup
  3. JUnit and Spring Testextension model, TestContext framework

3 layers, from the top down. Step through them one at a time.

We keep the number of composed annotations small, for example @ServiceTest for full integration tests and @DomainTest for plain unit tests with just the clock. Services that need something unusual combine the individual extensions themselves.

4Versioning, Documenting and Testing the Library

The library is a product used by 50 teams, so it gets semantic versioning, where a major version signals a breaking change, a changelog, and a short guide with one example per extension. Releases are tested in two ways: the library's own tests, and a set of sample projects, small Spring Boot applications that use the library the way real services do. Before each release, the samples run against the new version, which catches problems such as an extension that breaks with a new Spring Boot version.

Adoption should be gradual. We migrate two or three services first, measure how much setup code disappears, for example 120 lines per service, and fix rough edges before announcing it widely.

5Not Hiding Too Much

The biggest risk of a shared test library is magic. If @ServiceTest silently inserts 40 rows of reference data, sets a security context and changes the time zone, then a test that fails because of one of those becomes very hard to understand, especially for a new team member who never looked inside the library.

So we follow a few rules. Fixtures are visible: tests receive them as parameters, so reading the test shows what it uses. Defaults are documented and printed in failure messages where possible, for example the fixed time. Each extension does one thing and can be used alone. And we resist adding features that only one service needs; those belong in that service.

Good and risky library features
FeatureWhy
Clock injected as a parametervisible in the test signature
Cleanup of tables the test touchedprevents leaks, no hidden data
Hidden seed data inserted for every testrisky: failures depend on unseen rows
Global time zone or locale changesrisky: affects code far from the test

6Follow-up questions

Why not a base test class instead of extensions?

A base class forces one inheritance chain, so a service that needs only the clock still inherits everything, and a class can extend only one base. Extensions compose: a test uses exactly the ones it needs, and composed annotations give the convenience of a base class without the coupling.

How would the clock work with Spring beans?

For integration tests, the library can also provide a Spring test configuration that registers a fixed Clock bean, and the extension resolves the same instance from the application context, so the code under test and the test agree on the time. Services must inject Clock rather than calling Instant.now() directly, which the library's guide should state.

How do you handle parallel execution in these extensions?

Each extension keeps per-test state in the ExtensionContext Store, not in fields, and the cleanup extension declares a resource lock or works on a per-test schema, so parallel tests do not truncate each other's tables.

7Conclusion

In this answer, we turned repeated setup across 50 services into a small library of independent JUnit extensions: a ParameterResolver for a fixed Clock, another for test users, and an after-each cleanup for the database. Composed annotations such as @ServiceTest combine them with Spring test annotations and defaults for the common cases. The library is versioned, documented and tested against sample projects, and it keeps fixtures visible instead of hiding behaviour that makes failures hard to understand.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start by listing what the services actually repeat, which here is a fixed clock, test users and database cleanup, and turn each into its own small JUnit extension in a shared test library.

Then, if they want more

The clock and the users would be ParameterResolvers, so a test just declares a Clock or a TestUser parameter and gets a fixture, which keeps it visible in the test signature. Cleanup would be an AfterEachCallback that truncates the tables the test used, rather than a hand-maintained list.

On top I would offer a couple of composed annotations, for example @ServiceTest that combines @SpringBootTest, the test profile, a tag and the three extensions, and @DomainTest with just the clock. Teams with unusual needs combine the extensions themselves.

Because fifty teams depend on it, I would treat it as a product: semantic versioning, a changelog, a short guide with examples, and sample projects that run against every release so a Spring Boot upgrade does not break everyone at once. I would migrate two or three services first and measure how much setup disappears.

The thing I would guard against is magic, like hidden seed data or global time zone changes, because then a failing test becomes hard to understand. Each extension does one thing and per-test state lives in the Store, so it works with parallel runs.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JUnit user guide
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your organisation's tests repeat the same setup for clocks, test users and database...": https://scrivio-interview-library.netlify.app/java/junit/94001c7e6c/