Spring tests skip with ApplicationContext failure threshold (1) exceeded
Your error
java.lang.IllegalStateException: ApplicationContext failure threshold (1) exceeded: skipping repeated attempt to load context for ...1 more line it can showFix it fast
Most common cause, 1 of 3A slice test such as @WebMvcTest lacks a mock for the controller's service
Declare each collaborator the controller needs as a @MockitoBean field in the test.
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@MockitoBean
private OrderService orderService;
}Short answer
ApplicationContext failure threshold (1) exceeded is a follow-up message. Since Spring Framework 6.1 a failing test context is tried once, and every later test with the same configuration skips with this line. Read the Caused by in the first failed test and fix that: usually a missing mock in a slice test, no test database, or an unset property.
Which cause do you have?
Find the report that holds Failed to load ApplicationContext, then run only that class so its full stack trace is the first thing printed. The deepest Caused by names the real problem.
grep -l "Failed to load ApplicationContext" target/surefire-reports/*.txt
mvn -Dtest=OrderControllerTest testThen find the line you see in this list and open its fix:
- 1A slice test such as @WebMvcTest lacks a mock for the controller's service
The deepest Caused by is a NoSuchBeanDefinitionException that names one of your service or repository classes - 2The test context needs a database that the test run does not have
The cause chain of the first failing class holds a DataSourceBeanCreationException such as Failed to determine a suitable driver class, and its log says Failed to configure a DataSource: 'url' attribute is not specified - 3A ${...} property used by a bean has no value in the test environment
The deepest Caused by is a PlaceholderResolutionException reading Could not resolve placeholder, followed by the key's name
What does the error look like?
java.lang.IllegalStateException: ApplicationContext failure threshold (1) exceeded: skipping repeated attempt to load context for ...java.lang.IllegalStateException: Failed to load ApplicationContext for ...
Cause 1 of 3
A slice test such as @WebMvcTest lacks a mock for the controller's service
You seeThe deepest Caused by is a NoSuchBeanDefinitionException that names one of your service or repository classes
Check
Here the threshold message follows a missing bean. @WebMvcTest scans controllers, advice, converters and filters, but not regular @Component or @Service classes, so a controller that needs a service cannot be built unless the test supplies one.
grep -rn "@WebMvcTest\|@WebFluxTest" src/test/javaFix
Declare each collaborator the controller needs as a @MockitoBean field in the test. In Spring Boot 4 the annotation lives in spring-test, not in a Boot package.
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@MockitoBean
private OrderService orderService;
}Verify
The class runs on its own without the bean error, and the other classes that shared its context stop reporting the threshold.
mvn -Dtest=OrderControllerTest testConfirmed by docs.spring.io
Cause 2 of 3
The test context needs a database that the test run does not have
You seeThe cause chain of the first failing class holds a DataSourceBeanCreationException such as Failed to determine a suitable driver class, and its log says Failed to configure a DataSource: 'url' attribute is not specified
Check
A @SpringBootTest starts the whole application, including its DataSource. On a CI runner or a laptop with no database configured for tests, the context cannot start, the first class fails, and the rest of the classes on that configuration hit the threshold.
ls src/test/resources/ && grep -rn "spring.datasource" src/test/resources src/main/resourcesFix
Give tests their own database. With the spring-boot-testcontainers test dependency, a container field marked @ServiceConnection supplies the connection details automatically; putting H2 on the test classpath is the lighter option the failure analyzer itself suggests.
@SpringBootTest
@Testcontainers
class OrderRepositoryIT {
@Container
@ServiceConnection
static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:17");
}Verify
The test log shows the container starting before the context, and the DataSource error is gone.
mvn -Dtest=OrderRepositoryIT testConfirmed by docs.spring.io
Cause 3 of 3
A ${...} property used by a bean has no value in the test environment
You seeThe deepest Caused by is a PlaceholderResolutionException reading Could not resolve placeholder, followed by the key's name
Check
Values that come from environment variables or a profile file on the server are often absent when tests run. Any bean that injects such a key with @Value stops the context from loading.
grep -rn '\${' src/main/java | grep @ValueFix
Supply the missing keys for tests only, either in a properties file under src/test/resources or inline with @TestPropertySource on the test class.
@SpringBootTest
@TestPropertySource(properties = { "payments.api-key = test-key", "payments.base-url: http://localhost:9999" })
class CheckoutServiceTest {
}Verify
The context loads and the placeholder error no longer appears in the surefire report.
mvn -Dtest=CheckoutServiceTest testConfirmed by docs.spring.io
How do I stop it coming back?
- Fix the first failing test class before reading any later ones; the threshold lines after it carry no new information.
- Keep slice tests narrow: one controller per @WebMvcTest, with every collaborator declared as a @MockitoBean.
- Give integration tests a Testcontainers database so CI never depends on a database that only exists on a developer machine.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro.