Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

Should integration tests in your organisation use Testcontainers, shared test environments, or both? Give your reasoning.

Both, with clear roles. Testcontainers should be the default for service-level integration tests, because every build gets isolated, reproducible infrastructure matching production versions. Shared environments are still needed for cross-service and end-to-end checks, but they suffer from contention and drift, so we keep a smaller set of end-to-end tests there. To make the policy work, we provide approved images through an internal registry mirror, and we budget for CI agents that can run containers.

Why this question is asked

Many organisations grew up with one shared integration environment where every team deploys and tests, and they know its problems well: a broken deployment from one team blocks everyone, test data collides, and versions drift from production. Containers offer an alternative, but not a complete one. Interviewers ask this leadership question to see whether we can weigh both, set a policy that teams can follow, and plan for what the policy costs. In this answer, we will look at what each approach is good and bad at, decide on a split with reasons, and then cover the practical parts: images, CI capacity and how to roll the policy out.

1A Quick Recap of the Two Approaches

With Testcontainers, each build starts the infrastructure it needs as Docker containers inside the build itself: a PostgreSQL, a Kafka broker, a Redis. The containers live only as long as the test run and belong to that build alone. Because the image tag is pinned, for example postgres:17.2, the same version runs on every laptop and every CI agent.

A shared test environment is a long-lived set of deployed services and infrastructure, often called staging, integration or QA, that many teams use. It is closer to production in topology: real networking, real service discovery, the actual versions of neighbouring services, sometimes real cloud services that cannot run in a container.

2What Testcontainers Is Good and Bad At

Its strengths come from isolation. Each build has its own database, so no other team's test can change our data or lock our tables. A failure is reproducible on a laptop by running the same test. Versions match production because we choose the image, and upgrading PostgreSQL in tests is a one-line change that we can try in a branch.

Its limits come from scope. A container holds our infrastructure, not other teams' services. We can stub a neighbour's API with WireMock, but the stub encodes our assumptions about it. Some infrastructure is hard to containerise faithfully, such as managed cloud services with their own permissions and quotas. And it needs Docker or a compatible runtime on every CI agent and developer machine, which is a real platform cost.

3What Shared Environments Are Good and Bad At

A shared environment tests what nothing else can: that our service, deployed with its real configuration, works with the real versions of the services around it, through the real network, gateways and identity provider. That is where contract mismatches, misconfigured routes and permission problems show up.

Its weaknesses come from being shared. Contention: ten teams deploying to the same environment means someone's half-finished change breaks someone else's tests. Data collisions: tests that assume a clean customer table fail when another team's tests created rows. Drift: over months, the environment's versions, configuration and data diverge from production, so a pass there means less than it seems. Diagnosis is slow, because a failure might come from any of thirty services.

Testcontainers compared with shared environments
AspectTestcontainersShared environment
Isolationper build, no interferencecontention between teams
Reproducibilitysame on laptop and CIdepends on current state
Version matchpinned images, chosen by usdrifts over time
Covers other servicesonly through stubsreal neighbours
Feedback speedminutes, inside the buildafter deployment, slower
Main costcontainer runtime on CIupkeep and coordination

4The Policy: Containers for Services, a Few End-to-End Tests

Putting the two together gives a clear split. Every service tests its own behaviour with its own infrastructure in Testcontainers: repositories, migrations, message producers and consumers, caching, and its HTTP API with stubbed neighbours. These run on every pull request and must pass before merge. Most defects in data access and messaging are caught here, quickly and reproducibly.

Cross-service behaviour is covered in two layers. Contract tests, for example consumer-driven contracts, check that our stubs of a neighbour match what the neighbour actually provides, without needing a shared environment. Then a small set of end-to-end journeys runs in the shared environment after deployment: placing an order, paying, receiving a confirmation. A handful to a few dozen journeys, not thousands of tests, because each one is slow and expensive to diagnose.

The reason for keeping the shared environment small in the test plan is that its costs grow with use. Fewer tests there means less contention, fewer collisions and a faster signal that is still meaningful.

Step throughWhere each kind of integration test runs
  1. End-to-end journeysshared environment, after deployment, a few dozen
  2. Contract testsverify stubs against the real provider's contract
  3. Service integration testsTestcontainers on every pull request
  4. Unit testsno infrastructure

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

5Approved Images Through an Internal Registry Mirror

If every build pulls postgres and kafka images from Docker Hub, we meet rate limits, depend on an external service for every build, and have no control over what we run. So the platform team provides an internal registry mirror with approved images, at versions that match production, scanned for vulnerabilities. Testcontainers can be pointed at it globally, so test code does not change.

Java
# testcontainers.properties on CI agents (or the TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX variable)
hub.image.name.prefix=registry.internal.example.com/mirror/
// test code keeps the public name; Testcontainers adds the prefix
new PostgreSQLContainer<>("postgres:17.2-alpine"); // pulled from registry.internal.example.com/mirror/

The approved list also gives us a lever for version alignment: when production moves to PostgreSQL 18, the platform team publishes the image and teams update one tag.

6Budgeting for CI and Rolling It Out

Containers need a container runtime on CI agents, which can mean Docker-in-Docker, a Docker socket, a rootless runtime, or a remote container service, each with security and cost trade-offs that the platform team must decide. Agents also need more memory and CPU, because a build now runs a database and a broker beside the tests. As a rough planning number, an agent running a Spring service with PostgreSQL and Kafka containers may need 4 to 8 GB more memory than one running unit tests only. That is a line in the budget, and leaving it out is how such policies fail: teams find the agents too small and quietly go back to mocks.

The rollout follows the usual path: a template project with the shared container configuration, the registry mirror, and agents sized for it; one or two pilot teams who measure flaky failures and defects caught; then the policy written into the testing standard. In the shared environment, we reduce the test count over time to the agreed journeys and give it an owner who keeps it close to production.

7Follow-up questions

Can ephemeral environments replace the shared one?

Partly. Creating a short-lived environment per pull request, for example in Kubernetes namespaces, removes contention and drift, but it costs more infrastructure and still needs neighbouring services. Many organisations use them for the most critical journeys and keep one shared environment for the rest.

What about developers without Docker locally?

The standard should name a supported container runtime for laptops, and the build should skip container tests with a clear message rather than fail obscurely. CI remains the place where they always run.

How do you keep the end-to-end set small?

By requiring each journey to justify itself with a risk that lower layers cannot cover, and by deleting tests that duplicate service-level tests. An owner reviews the set every quarter.

8Conclusion

In this answer, we compared Testcontainers and shared environments. Testcontainers gives each build isolated, reproducible infrastructure that matches production versions, while shared environments test real neighbouring services but suffer from contention and drift. The policy uses Testcontainers for service-level integration tests on every pull request, contract tests between services, and a small set of end-to-end journeys in the shared environment, supported by approved images through an internal registry mirror and CI agents budgeted to run containers.

What to say in the interviewAbout 80 seconds aloud

Open with

Both, but with different jobs. Testcontainers should be the default for service-level integration tests, because every build gets its own database or broker, nothing another team does can break it, a failure reproduces on a laptop, and pinned image tags keep the versions in line with production.

Then, if they want more

That is where most data access and messaging bugs get caught, on every pull request. Shared environments are still needed for what containers cannot show: our service deployed with real configuration, talking to the real versions of neighbouring services, through the real gateway and identity provider.

But they suffer from contention between teams, colliding test data and drift from production, so I would keep only a small set of end-to-end journeys there, a few dozen at most, and use contract tests to check our stubs of neighbours without needing the shared environment.

To make it work, the platform team provides approved images through an internal registry mirror, configured globally in Testcontainers, so we avoid Docker Hub limits and control versions. And we budget for CI agents that can run containers, with the right runtime and more memory, because if the agents cannot run them, teams will drift back to mocks.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 18
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.
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 "Should integration tests in your organisation use Testcontainers, shared test...": https://scrivio-interview-library.netlify.app/java/integration-testing/fc5e62832f/