Testing and Production Development· 40 min

Testcontainers, Docker, and CI/CD

Run realistic tests and package Java services for repeatable delivery.

Java interview lesson
Question 28 · Not reviewed

Testing · delivery pipeline

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Testcontainers, Docker, and CI/CD

Explain it aloud in your own words

Strong answer

Testcontainers starts real dependencies such as PostgreSQL, Kafka, or Redis in throwaway Docker containers from the test, so integration tests exercise the same engines that run in production. Docker then packages the service into an immutable image built once and promoted through environments. A CI/CD pipeline ties it together: compile, run unit and integration tests, scan, build the image, tag it with the commit, deploy progressively, and roll back on failing health signals.

Key points

  • @ServiceConnection (Boot 3.1+) wires container connection details into Spring without manual properties.
  • Reuse one container per test class or JVM to keep suites fast; isolate data per test instead.
  • Use a multi-stage or layered build with a small JRE base image and a non-root user.
  • Build the artifact once and promote the same image; configuration changes per environment, not the binary.

Interviewer looks for

Describe a pipeline you have run, how you keep integration tests fast and isolated, what makes a Docker image production-ready, and how a bad release is detected and rolled back.

Java example

1@SpringBootTest
2@Testcontainers
3class OrderRepositoryIT {
4
5  @Container
6  @ServiceConnection
7  static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");
8
9  @Autowired OrderRepository orders;
10
11  @Test
12  void findsRecentOrdersForCustomer() {
13    orders.save(TestOrders.placedBy(7L));
14    assertThat(orders.findRecent(7L, PageRequest.of(0, 10))).hasSize(1);
15  }
16}
17
18# Dockerfile
19FROM eclipse-temurin:21-jdk AS build
20WORKDIR /app
21COPY . .
22RUN ./mvnw -q -DskipTests package
23
24FROM eclipse-temurin:21-jre
25RUN useradd --system app
26USER app
27COPY --from=build /app/target/orders.jar /app/orders.jar
28ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "-jar", "/app/orders.jar"]

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

Why use Testcontainers instead of an in-memory database?

Strong answer

It runs the real database engine and version, so dialect-specific SQL, constraints, locking, migrations, and JSON types behave as in production. In-memory substitutes can make tests pass while real queries fail.

Q2Intermediate

How do you keep Testcontainers-based tests fast?

Strong answer

Share containers across tests with static fields or a singleton base class, enable reuse locally, start only the dependencies a test needs, and isolate data by truncating tables or using unique keys rather than restarting containers.

Q3Intermediate

What makes a Java Docker image production-ready?

Strong answer

A pinned, minimal JRE base image, a multi-stage or layered build, a non-root user, container-aware memory settings such as MaxRAMPercentage, health endpoints for probes, no baked-in secrets, and a tag tied to the commit for traceability.

Q4Intermediate

What stages would you put in a CI/CD pipeline for a Spring Boot service?

Strong answer

Compile and static checks, unit tests, integration tests with Testcontainers, dependency and image vulnerability scanning, building and pushing an immutable image, deploying to staging with smoke tests, then a progressive production rollout gated on metrics.

Q5Advanced

How do you deploy safely and roll back?

Strong answer

Use rolling, blue-green, or canary releases with readiness probes, keep database migrations backward compatible, watch error rate and latency during rollout, and redeploy the previous image tag automatically when health signals degrade.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.