Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 3

A 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.

Fix
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;
}
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Spring Framework 7.1 (threshold since 6.1), Spring Boot 4.2, Maven Surefire 3.6
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
grep -l "Failed to load ApplicationContext" target/surefire-reports/*.txt
mvn -Dtest=OrderControllerTest test

Then find the line you see in this list and open its fix:

  1. 1A slice test such as @WebMvcTest lacks a mock for the controller's serviceThe deepest Caused by is a NoSuchBeanDefinitionException that names one of your service or repository classes
  2. 2The test context needs a database that the test run does not haveThe 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
  3. 3A ${...} property used by a bean has no value in the test environmentThe 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.

Check
grep -rn "@WebMvcTest\|@WebFluxTest" src/test/java

Fix

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.

Fix
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.

Verify
mvn -Dtest=OrderControllerTest test

Confirmed by docs.spring.io

Did this fix it?

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.

Check
ls src/test/resources/ && grep -rn "spring.datasource" src/test/resources src/main/resources

Fix

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.

Fix
@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.

Verify
mvn -Dtest=OrderRepositoryIT test

Confirmed by docs.spring.io

Did this fix it?

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.

Check
grep -rn '\${' src/main/java | grep @Value

Fix

Supply the missing keys for tests only, either in a properties file under src/test/resources or inline with @TestPropertySource on the test class.

Fix
@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.

Verify
mvn -Dtest=CheckoutServiceTest test

Confirmed by docs.spring.io

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.