Integration testing and Testcontainers
22 questions in 6 subtopics.
Testcontainers
Before@ServiceConnection existed, how were container ports passed to Spring, and when is @DynamicPropertySource still needed?Show how to run a Spring Boot repository test against a real PostgreSQL using Testcontainers and @ServiceConnection.Your suite has 120 test classes each starting its own PostgreSQL and Kafka containers. How would you cut the time without making tests depend on each other?Should integration tests in your organisation use Testcontainers, shared test environments, or both? Give your reasoning.Testcontainers makes it easy to run real infrastructure in tests. Over years, what costs and risks come with depending on it across thousands of builds, and how do you manage them?Database tests
Why can a repository test pass on H2 but fail on PostgreSQL in production?A@DataJpaTest saves an entity that violates a unique constraint, but the test that expects an exception fails because nothing is thrown. Why?A @SpringBootTest with webEnvironment = RANDOM_PORT is annotated @Transactional, yet rows created via HTTP calls are not rolled back. Why?Your teams use H2 for repository tests in some services and Testcontainers in others, and several production incidents came from SQL differences. What standard would you set and how would you roll it out?Test data management
Compare@Sql scripts, test data builders and the Object Mother pattern for setting up data in Spring tests.Design a test data strategy for integration tests that run in parallel against one shared database container.Teams copy anonymised production data into test environments, but privacy reviewers now object. What alternatives would you propose?Regulators require that no personal data appears in non-production systems, but your integration tests and staging environments have used production copies for a decade. How would you plan the exit?Flaky tests
An asynchronous test sometimes fails because a message listener has not processed the event yet. The test usesThread.sleep(500). How would you fix it?About 3 percent of CI builds fail on random tests that pass on retry. How do you investigate and reduce flakiness?Over years, how do flaky tests affect an engineering organisation, and what policies would you put in place?HTTP service mocking with WireMock and MockWebServer
Write a Spring Boot test that stubs a downstream GET /customers/42 returning JSON with WireMock, and points the application'sRestClient at the stub. How do you verify the request was sent with the right header?Integration tests use WireMock stubs written by hand, and production failed because the real downstream API changed a field name. How do you keep HTTP mocks honest, and how do you test timeouts and retries?Across 150 services, teams mock dependencies differently: some call shared staging environments, some use WireMock, some mock HTTP clients in unit tests. What testing standard would you set for service-to-service calls?REST API testing with REST Assured
Write a REST Assured test against a Spring Boot app started withwebEnvironment = RANDOM_PORT that posts a JSON order and checks status 201, a Location header and the id in the body.Compare REST Assured, MockMvc, WebTestClient and the newer RestTestClient or MockMvcTester options in Spring for testing REST APIs. When would you pick each?Your API test suite of 2,000 REST Assured tests takes 50 minutes and runs against a shared environment. Releases wait for it. How would you restructure API testing?No question matches these choices.