Skip to content
Latchkey

Spring Boot "@SpringBootTest failed to load ApplicationContext" in CI

JUnit could not build the Spring context for a @SpringBootTest, so every test in the class errors before running. The failure is a normal startup error surfaced through the test framework; read its Caused-by.

What this error means

Tests error (not fail) with "java.lang.IllegalStateException: Failed to load ApplicationContext", followed by the same Caused-by you would see at app startup.

Spring Boot
java.lang.IllegalStateException: Failed to load ApplicationContext for
[WebMergedContextConfiguration@...]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'dataSource' ...

Common causes

A bean or resource fails only in CI

The context needs a DB, broker, or property that is present locally but missing on the runner, so the refresh throws.

The test profile is not the one you expect

CI activates a different (or no) profile, so config and beans differ and the context cannot assemble.

How to fix it

Trace the Caused-by like a startup error

  1. Read the innermost Caused-by under the IllegalStateException.
  2. Fix that root (provide the DB URL, activate the profile, define the bean).
  3. Re-run the failing test class.

Pin the test profile and provide its properties

Activate a dedicated test profile and supply everything it needs in CI.

src/test/java
@ActiveProfiles("test")
@SpringBootTest
class OrderFlowTest { }

How to prevent it

  • Give integration tests a self-contained test/ci profile.
  • Provide DB and broker via Testcontainers so the context always assembles.
  • Prefer slices over full context where a full app is not needed.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →