Skip to content

@MockitoBean should allow a custom answer #34956

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

Open
adrian-tarau opened this issue May 27, 2025 · 4 comments
Open

@MockitoBean should allow a custom answer #34956

adrian-tarau opened this issue May 27, 2025 · 4 comments
Labels
in: test Issues in the test module status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@adrian-tarau
Copy link

Currently, there is no way to request a few mocks, provide answers (to share common setups), and automatically inject these mocks where needed.

@MockitoBean should have a property called answer, of type Class<? extends Answer>, and use the class (create an instance) when a mock is created in MockitoBeanOverrideHandler.createMock.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 27, 2025
@sbrannen sbrannen added the in: test Issues in the test module label May 28, 2025
@sbrannen
Copy link
Member

Hi @adrian-tarau,

We don't consider it very common to configure a custom Answer with Mockito, and the documentation for Mockito also states something along those lines.

However, we do support the standard Answers enum from Mockito.

Are you familiar with @MockitoBean( answers = ... )?

If Answers does not meet your needs, what is it exactly that you are trying achieve when you say "to share common setups"?

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label May 28, 2025
@adrian-tarau
Copy link
Author

adrian-tarau commented May 29, 2025

@sbrannen I think Mockito says something like "in rare cases, etc., etc." :) However, it does not mean it is not needed or part of the API. @MockitoBean( answers = ... ) only provides default behaviours (also using an Answer). And by the way, RETURNS_SMART_NULLS should be used instead of RETURNS_DEFAULTS as a default value, as it allows for safer behavior (my 2 cents).

The purpose of using custom answers is to provide shared behaviours (just like Mockito does internally) across repositories and services. I do not want to keep repeating the same pattern across tests within modules. For example:

 @MockitoBean private RepositoryA repoA;
 @MockitoBean private RepositoryB repoB;

 @Autowired private ServiceA serviceA;

The service is created, mocks are injected, and since ServiceA implements InitializingBean, the repositories will be called before behaviors can be added. If I could attach behaviours in the annotation, the mock would be done using a common behaviour.

I can provide a patch if the enhancement is approved/accepted. There are just a few lines of code, and while it may not be helpful or useful for everyone, it might be beneficial in some cases.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 29, 2025
@adrian-tarau
Copy link
Author

@sbrannen, since the default answer in Mockito can be changed, please disregard RETURNS_SMART_NULLS vs RETURNS_DEFAULTS. Smart nulls are/should always be more useful.

@adrian-tarau
Copy link
Author

adrian-tarau commented May 30, 2025

Forgot to mention: I know I can use @ TestBean and take over the creation of the mocked bean, but it's too much boilerplate code. Ultimately, the method I will use creates a mock of the bean (in this case, the repository) with a shared answer. It would be much better to provide an answer class with @MockitoBean.

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 status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

3 participants