Skip to content

Conversation

@PainFu11y
Copy link

Problem:
Tests using @SpringBootTest are slower in Spring Boot 4.0 because
ApplicationContext is paused/restarted between test classes even
when they share the same context. This particularly affects tests
with expensive SmartLifecycle beans like Kafka consumers.

Solution:
Introduce @RetainApplicationContext annotation to mark test classes
whose context should not be paused/restarted. Update
DefaultTestContext.markApplicationContextUnused() to check for this
annotation before unregistering context usage.

Usage:
Annotate abstract test classes with @RetainApplicationContext to
retain context between subclasses, reducing overhead.

Closes gh-36044

Add @RetainApplicationContext annotation to prevent pausing and
restarting the ApplicationContext between test classes when
SmartLifecycle beans are expensive to stop/start. Update
DefaultTestContext.markApplicationContextUnused() to respect this
annotation.

Closes spring-projectsgh-36044

Signed-off-by: Painfu11y <333vahan777@gmail.com>
@bclozel
Copy link
Member

bclozel commented Dec 23, 2025

Thanks but the related issue is already assigned and the implementation chosen here does not match the intent of the team.

@bclozel bclozel closed this Dec 23, 2025
@bclozel bclozel added the status: declined A suggestion or change that we don't feel we should currently apply label Dec 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid pause/restart for application context in the TestContext framework

2 participants