Skip to content

The MockitoBean annotation is not processed when using a TestConfiguration annotated class #44496

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

Closed
umekong opened this issue Feb 28, 2025 · 3 comments
Labels
status: duplicate A duplicate of another issue

Comments

@umekong
Copy link

umekong commented Feb 28, 2025

This issue is currently being observed in version 3.4.3.
Here is quick bit of code to highlight issue

@SpringBootTest
class MockObjectTest {
    @Test
    void doNothing() {
        // test started up fine
    }

    @TestConfiguration
    static class InitMocks {
        @MockBean // deprecated and set for removal in 3.6.0
        private SomeObject mockBean;

        @MockitoBean
        private SomeObject mockitoBean;

        @PostConstruct
        public void init() {
            assertNotNull(mockBean); // successful
            assertNotNull(mockitoBean); // fails
        }
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 28, 2025
@wilkinsona wilkinsona added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 28, 2025
@wilkinsona
Copy link
Member

@TestConfiguration is meta-annotated with @Component which is why this works with @MockBean. @MockitoBean doesn't support such injection. spring-projects/spring-framework#34415 has a suggestion for an alternative arrangement that may help.

@umekong
Copy link
Author

umekong commented Feb 28, 2025

Should the Javadoc be updated to reflect that MockitoBean is not a like-for-like replacement for the deprecated MockBean then?
Also @wilkinsona, I would need to verify but the suggestion in spring-projects/spring-framework#34415 would not work in scenarios where the TestConfiguration class has been introduced because the MockitoBean needs to be configured before usage by another Bean within the Spring context.

@wilkinsona
Copy link
Member

#43348 is tracking some documentation improvements.

the MockitoBean needs to be configured before usage by another Bean within the Spring context.

With that requirement, whether you're using @MockBean or @MockitoBean, we recommend using a @Bean method that creates, configures, and returns a Mockito mock directly instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants