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

Support mocking of @Singleton beans #8983

Closed
miron4dev opened this issue Apr 30, 2020 · 7 comments
Closed

Support mocking of @Singleton beans #8983

miron4dev opened this issue Apr 30, 2020 · 7 comments
Labels
kind/enhancement New feature or request triage/wontfix This will not be worked on

Comments

@miron4dev
Copy link

Description
As a developer, I want to mock @Singleton beans.
Sometimes, it's not possible to annotate a bean by @ApplicationScoped, e.g. in the case described here #8960

@miron4dev miron4dev added the kind/enhancement New feature or request label Apr 30, 2020
@zenonwch
Copy link

And for @InjectSpy as well.
Pls.

@fabasoad
Copy link

fabasoad commented Jun 9, 2021

I just faced this problem. I have added vertx extension to use EventBus class. And it is defined as @Singleton:
https://github.com/quarkusio/quarkus/blob/main/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxProducer.java#L34
So I can't mock it in my test class. Are there any workarounds on how can I mock @Singleton annotated class' instances?

@geoand
Copy link
Contributor

geoand commented Jul 27, 2021

You can use @InjectMock(convertScopes=true) if absolutely necessary

@geoand geoand closed this as completed Jul 27, 2021
@geoand geoand added the triage/wontfix This will not be worked on label Jul 27, 2021
@takesson
Copy link

takesson commented Nov 8, 2021

Does convertScopes work for Named beans?

I am getting exactly the same error regardless of convertScopes setting:

Invalid use of io.quarkus.test.junit.mockito.InjectMock - the injected bean does not declare a CDI normal scope but: javax.inject.Singleton. Offending field is solrClient of ...

Quarkus 2.4.1.

	@InjectMock(convertScopes=true)
	@Named("repositem")
	SolrClient solrClient;

@geoand
Copy link
Contributor

geoand commented Nov 8, 2021

Can you please open a new issue? Attaching an example application that fails like you mention would be very helpful

@takesson
Copy link

takesson commented Nov 9, 2021

I now understand the issue better.

  • I have confirmed that convertScopes works fine in combination with Named.
  • The issue I saw was the final method that forced the use of Singleton in the first place. We have Named beans that extend a class with a final method and tons of other methods (out of our control).

In that situation, the error message is identical regardless of convertScopes setting so it wasn't obvious if convertScopes was in effect. I now understand its limitations better.

@gian1200
Copy link
Contributor

gian1200 commented Feb 7, 2024

For anyone working with Quarkus 3 reaching this point:

io.quarkus.test.junit.mockito.InjectMock was replaced with io.quarkus.test.InjectMock. convertScopes doesn't exist in the new class. To get the same behavior, you should implement it this way:

@InjectMock
@MockitoConfig(convertScopes = true) 
EventBus eventBus; // or your favorite singleton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request triage/wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

6 participants