spring-projects / spring-projects/spring-framework

`ContextCustomizer` does not respect lifecycle best practices

Open
#31,527 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: test type: enhancement
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

org.springframework.test.context.ContextCustomizer has been introduced to allow implementations to customize the context programmatically. Looking at implementations of the interface, it is primarily used in Spring Boot.

The unique method states the following:

Customize the supplied ConfigurableApplicationContext after bean definitions have been loaded into the context but before the context has been refreshed.

There are two main use cases I can see:

  • Mutate the environment to enable/disable something
  • Contribute more (test-related) beans

Because the context is not refreshed, the second bit should really register a bean definition and let the container creates it like any regular bean. ConfigurableApplicationContext does not expose a way to register a bean definition and it does this on purpose to separate the layer of responsability. As a result, we can see a number of implementations using a cast to get access to the registry.

The other problem is that by providing ConfigurableApplicationContext, the implementation has access to a lot more than it should, in particular on a context that has not been refreshed yet. This leads to issues like https://github.com/spring-projects/spring-kafka/issues/2870.

I don't know if the problem is big enough to consider a refactoring but I think the casting does not look right.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with org.springframework.test.context.ContextCustomizer and inspect the implementations mentioned in Spring Boot, focusing on casts used to access bean-definition registration and on the supplied ConfigurableApplicationContext. Review the related spring-kafka issue for the lifecycle concern; done would require an agreed refactoring that preserves environment customization and supports test bean registration without exposing inappropriate context operations.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
testing
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.