Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce missing tests for Bean Override features #33742

Closed
13 tasks done
sbrannen opened this issue Oct 18, 2024 · 1 comment
Closed
13 tasks done

Introduce missing tests for Bean Override features #33742

sbrannen opened this issue Oct 18, 2024 · 1 comment
Assignees
Labels
in: test Issues in the test module type: task A general task
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Oct 18, 2024

Overview

Over the past few weeks it has become apparent that our test suite does not fully cover all features of the Bean Override support in the TestContext framework.

To remedy that, we should introduce tests to cover corner cases and features that are currently not tested. As a starting point, we can adopt several of the tests for @MockBean and @SpyBean from Spring Boot's test suite.

Features to Test

The following is a list of features we should make sure we test before Spring Framework 6.2 GA.

  • Combining SpringExtension and MockitoExtension on the same test class, including support for @Captor combined with @MockitoSpyBean.
  • @MockitoBean, @MockitoSpyBean, and @TestBean with @DirtiesContext "before method" modes.
  • @MockitoBean with JUnit 4 and SpringMethodRule.
  • @MockitoBean and @MockitoSpyBean with @ContextHierarchy.
  • @MockitoBean and @MockitoSpyBean with generics and parameterized types.
  • @MockitoBean replacement of a bean with @Async methods.
  • @MockitoBean replacement of a scoped proxy.
  • @MockitoBean replacement of a Spring AOP proxy.
  • @MockitoSpyBean with circular dependencies.
  • Spring AOP proxy that wraps a Mockito spy created via @MockitoSpyBean.
  • @MockitoSpyBean on a field with generics can be used to replace an existing bean with matching generics that's produced by a FactoryBean that's programmatically registered via an ImportBeanDefinitionRegistrar.
  • @⁠MockitoBean and @⁠MockitoSpyBean can select a single candidate bean to override when there are multiple candidates that match the required type: via an explicit bean name, @⁠Qualifier, or @⁠Primary on one of the candidate beans.
  • Mock reset support for beans within the ApplicationContext configured via MockReset.before() and MockReset.after().

Related Issues

@sbrannen sbrannen added in: test Issues in the test module type: task A general task labels Oct 18, 2024
@sbrannen sbrannen added this to the 6.2.0 milestone Oct 18, 2024
@sbrannen sbrannen self-assigned this Oct 18, 2024
@snicoll snicoll modified the milestones: 6.2.0-RC3, 6.2.0 Oct 22, 2024
sbrannen added a commit that referenced this issue Oct 23, 2024
Use cases not yet supported with @⁠MockitoSpyBean are currently @⁠Disabled.

See gh-33742
sbrannen added a commit that referenced this issue Oct 29, 2024
This commit introduces a test which verifies that @⁠MockitoSpyBean on a
field with generics can be used to replace an existing bean with
matching generics that's produced by a FactoryBean that's
programmatically registered via an ImportBeanDefinitionRegistrar.

However, the test is currently @⁠Disabled until the fix for
spring-projects/spring-boot#40234 has been
ported to Spring Framework.

See gh-33742
sbrannen added a commit that referenced this issue Oct 30, 2024
This commit introduces integration tests which verify that Bean
Overrides (for example, @⁠MockitoBean and @⁠MockitoSpyBean) can select
a single candidate bean to override when there are multiple candidates
that match the required type.

To "select" the desired bean, these tests rely on one of the following.

- explicit bean name in the bean override annotation

- explicit @⁠Qualifier on the bean override field

- explicit @⁠Primary on one of the candidate beans

However, the @⁠Primary tests are currently @⁠Disabled until @⁠Primary
is honored in the Spring TestContext Framework.

See gh-33742
sbrannen added a commit that referenced this issue Oct 30, 2024
This commit verifies that MockReset.before() and MockReset.after() are
supported for beans within the ApplicationContext.

However, the test class must declare a field annotated with either
@⁠MockitoBean or @⁠MockitoSpyBean in order for the MockReset feature to
be triggered.

See gh-33742
@sbrannen
Copy link
Member Author

After porting numerous tests from Spring Boot's test suite and introducing additional tests (see Related Issues and linked commits above), I am closing this task with the hope that we now have better coverage of the supported features for Bean Overrides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: task A general task
Projects
None yet
Development

No branches or pull requests

2 participants