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

Deprecate @MockBean and @SpyBean #39860

Closed
philwebb opened this issue Mar 8, 2024 · 9 comments
Closed

Deprecate @MockBean and @SpyBean #39860

philwebb opened this issue Mar 8, 2024 · 9 comments
Labels
status: superseded An issue that has been superseded by another

Comments

@philwebb
Copy link
Member

philwebb commented Mar 8, 2024

With spring-projects/spring-framework#29917 fixed we should consider deprecating our versions.

@philwebb philwebb added this to the 3.x milestone Mar 8, 2024
@philwebb philwebb added the type: enhancement A general enhancement label Mar 8, 2024
@wanger26
Copy link
Contributor

@philwebb do we want to target release 2.3.0 with this change? Also are we thinking of marking those for removal at this time?

@philwebb
Copy link
Member Author

We'll probably look at this in Spring Boot 3.4.0 which will be the first release to build on Spring Framework 6.2. I'm not sure yet about when well mark the existing annotations for removal.

@wanger26
Copy link
Contributor

Okay sounds good. I can take this one on. Do we want to take this opportunity to migrate our own usages of those two annotations over to the new spring framework ones?

wanger26 added a commit to wanger26/spring-boot that referenced this issue Mar 10, 2024
… classes

- Deprecate Deprecate @MockBean and @SpyBean in favor of Spring Framekworks 6.2.0 @MockitoBean and @MockitoSpy. See spring-projects/spring-framework@e1bbdf0
- Migrate usages of @MockBean and @SpyBean to @MockitoBean an d @MockitoSpy

spring-projects#39860

Signed-off-by: Jakob Wanger <[email protected]>
wanger26 added a commit to wanger26/spring-boot that referenced this issue Mar 10, 2024
- Deprecate Deprecate @MockBean and @SpyBean in favor of Spring Framekworks 6.2.0 @MockitoBean and @MockitoSpy. See spring-projects/spring-framework@e1bbdf0
- Migrate usages of @MockBean and @SpyBean to @MockitoBean an d @MockitoSpy

spring-projects#39860

Signed-off-by: Jakob Wanger <[email protected]>
@philwebb
Copy link
Member Author

Closing in favor of PR #39864 which we'll merge sometime after we create the 3.3.x branch. Thanks @wanger26!

@philwebb philwebb closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2024
@philwebb philwebb added status: superseded An issue that has been superseded by another and removed type: enhancement A general enhancement labels Mar 11, 2024
@philwebb philwebb removed this from the 3.x milestone Mar 11, 2024
@OrangeDog
Copy link
Contributor

What is the replacement for @MockBean on a type? For example, this was very useful:

@TestConfiguration
@Import({ /* some config shared by tests */ })
@MockBean({ /* some beans that need to be mocked in shared tests */ })
public class SharedWebTestConfig {
    // ...
}

@WebMvcTest(SomeController.class)
@Import(SharedWebTestConfig.class)
public class SomeTetsts {
    // ...
}

Now the only solution is to make the mock beans manually?

@TestConfiguration
@Import({ /* some config shared by tests */ })
public class SharedWebTestConfig {
    @Bean
    public BeanClass myBean() {
        return Mockito.mock(BeanClass.class);
    }
    // ...
}

@wilkinsona
Copy link
Member

I don't believe Framework has a direct equivalent although you don't have to resort to declaring the mock beans manually. You can use @MockitoBean on fields in a test class, its type hierarchy and, if it's a nested test class, its enclosing class hierarchy so you could move your mocks into a class that SomeTests extends. If you'd like to see @MockitoBean support something beyond this, please open a Framework issue.

@sbrannen
Copy link
Member

What Andy said is correct: Spring Framework does not support @MockitoBean at the type-level.

See also spring-projects/spring-framework#29917 (comment).

@OrangeDog
Copy link
Contributor

OrangeDog commented Nov 20, 2024

Thanks for the confirmation. The trouble with using inheritance is that you can only do it once, while all the annotation-driven config is composable.

The @Bean public Foo foo() { return mock(Foo.class); } also doesn't actually work in some cases, as the added bean proxies prevent any mock invocations.

@nmck257
Copy link

nmck257 commented Nov 20, 2024

fyi @OrangeDog, other readers -- I've opened a standalone issue in Spring Framework for this topic:
spring-projects/spring-framework#33925
if you've got other distinct usage patterns, feel free to add that context to the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

6 participants