Skip to content

Conversation

@HuitaePark
Copy link
Contributor

@HuitaePark HuitaePark commented Jan 14, 2026

Related Issue

Closes gh-46853

Goal

Allow overriding the MockServerRestClientCustomizer bean created by MockRestServiceServerAutoConfiguration.

This is particularly useful for scenarios in 'RestClientTest' where customization of the mock server behavior is required, such as enabling content buffering via setBufferContent(true), which was previously overridden by the default auto-configuration.

Background

Currently, MockRestServiceServerAutoConfiguration unconditionally defines a MockServerRestClientCustomizer bean:

@Bean
MockServerRestClientCustomizer mockServerRestClientCustomizer() {
    return new MockServerRestClientCustomizer();
}

Because of this unconditional registration, if a user attempts to provide their own MockServerRestClientCustomizer bean to customize the MockRestServiceServer, the auto-configured bean may take precedence or cause conflicts. This prevents users from applying necessary configurations like enabling the buffering of response bodies.

Approach

Apply conditional registration:

Added @ConditionalOnMissingBean to the mockServerRestClientCustomizer bean definition in MockRestServiceServerAutoConfiguration.

This ensures that the auto-configuration backs off if a user has already defined a MockServerRestClientCustomizer bean in the application context.

Verification

I introduced a new test class MockRestServiceServerAutoConfigurationTests using ApplicationContextRunner to verify the fix.

The tests cover two scenarios:

  1. Default Behavior: Verifies that MockServerRestClientCustomizer is registered by the auto-configuration when no user bean is present.

  2. Override Behavior: Verifies that the auto-configuration backs off when a user-provided MockServerRestClientCustomizer is present, ensuring the user's bean is used.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 14, 2026
Copy link
Member

@wilkinsona wilkinsona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for the PR, @HuitaePark. I've left some comments for your consideration.

@HuitaePark
Copy link
Contributor Author

HuitaePark commented Jan 14, 2026

@wilkinsona Thanks for the review. I've applied the changes.

Previously, MockRestServiceServerAutoConfiguration unconditionally defined a
MockServerRestClientCustomizer bean, which prevented user overrides.

This commit adds @ConditionalOnMissingBean to ensure the auto-configuration
backs off when a user-provided customizer is present.

Closes spring-projectsgh-46853

Signed-off-by: HuitaePark <qkrgmlxo3174@gmail.com>
@HuitaePark HuitaePark requested a review from wilkinsona January 16, 2026 03:55
@wilkinsona wilkinsona self-assigned this Jan 16, 2026
@wilkinsona wilkinsona added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 16, 2026
@wilkinsona wilkinsona added this to the 4.1.x milestone Jan 16, 2026
@wilkinsona wilkinsona changed the title Allow user-override of MockRestServiceServer beans Allow beans created in MockRestServiceServerAutoConfiguration to be overridden Jan 16, 2026
@wilkinsona wilkinsona changed the title Allow beans created in MockRestServiceServerAutoConfiguration to be overridden Allow beans created in MockRestServiceServerAutoConfiguration to be replaced by user-provided alternatives Jan 16, 2026
@wilkinsona wilkinsona modified the milestones: 4.1.x, 4.1.0-M1 Jan 16, 2026
wilkinsona pushed a commit that referenced this pull request Jan 16, 2026
Previously, MockRestServiceServerAutoConfiguration unconditionally
defined its beans, preventing the user from providing an alternative.

This commit adds @ConditionalOnMissingBean to ensure the auto-configuration
backs off when a user-provided alternatives are present.

See gh-48825

Signed-off-by: HuitaePark <qkrgmlxo3174@gmail.com>
@wilkinsona
Copy link
Member

Thanks very much, @HuitaePark.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow beans created in MockRestServiceServerAutoConfiguration to be overridden

3 participants