Quick revision
The short answer
First we read the root cause at the bottom of the stack trace to find the bean that calls the payment API during startup, and if possible we move that call out of the constructor or @PostConstruct. Then we replace the client in tests with @MockitoBean or a test @Bean, or point its URL at a local stub such as WireMock through a test property or @DynamicPropertySource, and use a test profile to switch off integrations that are not under test.
What to remember
In this answer, we read the root cause at the bottom of the stack trace and found a bean calling the payment API in @PostConstruct. We moved that call out of startup where possible, then kept the API out of tests by replacing the client with @MockitoBean or a test bean, or by pointing its URL at WireMock through @DynamicPropertySource, and by switching off unrelated integrations with a test profile. We also saw why one failure repeats for every later test class.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we read the root cause at the bottom of the stack trace to find the bean that calls the payment API during startup, and if possible we move that call out of the constructor or @PostConstruct. Then we replace the client in tests with @MockitoBean or a test @Bean, or point its URL at a local stub such as WireMock through a test property or @DynamicPropertySource, and use a test profile to switch off integrations that are not under test.
Why this question is asked
A context that cannot start takes every test with it, so this kind of failure turns a whole suite red at once and blocks every merge. It usually appears on CI first, because developers' machines can reach the sandbox API and CI runners cannot. Interviewers ask it because the fix shows two skills: reading Spring's long error output, and designing beans so tests do not depend on the outside world. In this answer, we will reproduce the failure, find the root cause, fix the design, and then look at three ways to keep the payment API out of tests.
1Reproducing the Failure
Suppose a payment client checks its connection while it is being created, which feels like a sensible safety check.
On a developer laptop with network access this succeeds. On CI, where outbound traffic to the payment provider is blocked, the call fails, bean creation fails, and the test reports Failed to load ApplicationContext.
2Reading the Root Cause
The top of the error is generic. The useful part is the last Caused by line, at the bottom of a long chain of wrapped exceptions.
Notice how the chain names the bean, paymentClient, the phase, its init method, and the real reason, a connect timeout. There is a second effect worth knowing: since Spring Framework 6.1, the TestContext framework remembers that loading this configuration failed and makes later test classes with the same configuration fail fast, with a message that the failure threshold was exceeded, instead of retrying the slow startup each time. So 200 failing classes usually have one cause, and we only need to read the first full stack trace.
3Fix the Design: No Remote Calls During Construction
The best fix is in the production code. A bean that calls a remote system while it is being created makes the whole application's startup depend on that system, in tests and in production. If the payment provider is down for a minute during a deploy, our service will not start at all.
So we move the check out of startup. A health indicator for Spring Boot Actuator reports the payment API's status without blocking startup, and the first real payment call fails with a clear error if the API is unreachable. If the team insists on a startup check, it can run in an ApplicationReadyEvent listener guarded by a property that tests turn off.
4Fix 1: Replace the Client With a Mock
When the test is not about payments, we replace the bean. @MockitoBean puts a Mockito mock in the context instead of the real PaymentClient, so its @PostConstruct never runs.
For a hand-written fake, we declare a test @Bean in a @TestConfiguration that overrides the client, which is useful when many tests share the same fake behaviour. Keep in mind that each different set of mocked beans gives a different cached context, so it pays to mock the same beans the same way across classes.
5Fix 2: Point the Client at a Local Stub
When we want the real client code, its JSON mapping and error handling, under test, we keep the bean and point it at WireMock, a small HTTP server that answers with responses we define. @DynamicPropertySource lets us set the URL after WireMock has picked its port.
- Read the last Caused bynames the bean and the real error
- Is it a remote call at startup?YesMove it out of construction if we canNoFix the configuration error it names
- Is this dependency under test?YesStub it with WireMock via a test propertyNoReplace it with @MockitoBean or a test bean
- Turn off unrelated integrationstest profile with
@ActiveProfiles
4 steps in order. Play them, or step through them one at a time.
Notice that the property is supplied as a lambda, so it is read only when the context needs it. This works because the client already reads its URL from payment.base-url; a URL hard-coded in Java would leave tests no way in.
6Fix 3: A Test Profile for Integrations Not Under Test
Larger services have several integrations, and most tests touch none of them. A test profile, activated with @ActiveProfiles("test") on a shared base class, can set an application-test.yml with payment.enabled: false, and the payment beans can be guarded with @ConditionalOnProperty or @Profile("!test") so they are not created at all. Other integrations get the same treatment, and a test that needs one turns it back on explicitly.
7Follow-up questions
Why does the failure repeat for every later test class?
The TestContext framework caches contexts by configuration, and since Spring Framework 6.1 it also counts failures per configuration. After the first failure, later classes with the same configuration fail immediately instead of trying again, which saves time but makes the log look like many failures. The first full stack trace holds the cause.
Is mocking the client enough, or do we lose confidence?
Mocking is enough for tests about our own logic. The client itself deserves a few focused tests against WireMock with realistic responses, including errors and timeouts, and ideally a contract test with the provider. Together they cover what the mock hides.
Should CI be allowed to call the payment sandbox?
Only in a small, separate job, if at all. A sandbox can be slow, rate limited or down, and a test suite that depends on it fails for reasons unrelated to our change. The main suite should run with no network access to third parties.
8Conclusion
In this answer, we read the root cause at the bottom of the stack trace and found a bean calling the payment API in @PostConstruct. We moved that call out of startup where possible, then kept the API out of tests by replacing the client with @MockitoBean or a test bean, or by pointing its URL at WireMock through @DynamicPropertySource, and by switching off unrelated integrations with a test profile. We also saw why one failure repeats for every later test class.
Open with
First I would scroll to the last Caused by in the stack trace, because Failed to load ApplicationContext is just the wrapper. It will name the bean, for example paymentClient, and the real error, such as a connect timeout from a call in its constructor or @PostConstruct.
Then, if they want more
I would also explain to the team that the TestContext framework remembers context failures, so hundreds of failing classes usually share that one cause. My preferred fix is in production code: a bean should not call a remote API while it is being created, because then our service cannot even start in production when the provider is down.
I would move the check to an Actuator health indicator or a ready-event listener. For the tests, if payments are not under test I replace the client with @MockitoBean or a fake in a @TestConfiguration, so the real bean is never created.
If I want to test the client code itself, I keep it and point its base URL at WireMock, setting the property with @DynamicPropertySource once WireMock has its port. And for services with several integrations, a test profile with @ActiveProfiles disables the ones a test does not need. The goal is a suite that runs with no access to third-party systems.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- 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
- Spring Boot reference, Testing
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 "A team's @SpringBootTest suite fails on CI with 'Failed to load ApplicationContext'...": https://scrivio-interview-library.netlify.app/spring-boot/spring-boot-test/a7cfa7d82a/