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

Mockito extension not working with @QuarkusTest #24911

Closed
ivansenic opened this issue Apr 13, 2022 · 6 comments
Closed

Mockito extension not working with @QuarkusTest #24911

ivansenic opened this issue Apr 13, 2022 · 6 comments
Labels
area/testing kind/bug Something isn't working triage/wontfix This will not be worked on

Comments

@ivansenic
Copy link
Contributor

ivansenic commented Apr 13, 2022

Describe the bug

Having a test with:

@QuarkusTest
@ExtendWith(MockitoExtension.class)
class Test {

    @Captor
    ArgumentCaptor<Whatever> captor;
}

will not correctly utilize the MockitoExtension and Mockito annotated fields will not be initialized.

Expected behavior

  • org.mockito annotations are correctly initialized

Actual behavior

Seems that Mockito extension is initalizing the fields in the different test instance. Simple debugging I did showed that mockito correctly initialized fields in my test instance {GrpcClientsTest@10823}, while the actual instance being used in the test is {GrpcClientsTest@10895}.

How to Reproduce?

No response

Output of uname -a or ver

Linux ise-Precision-5550 5.13.0-39-generic #44~20.04.1-Ubuntu SMP Thu Mar 24 16:43:35 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Output of java -version

No response

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.8.0.Final

Build tool (ie. output of mvnw --version or gradlew --version)

pache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Maven home: /home/ise/.m2/wrapper/dists/apache-maven-3.8.4-bin/52ccbt68d252mdldqsfsn03jlf/apache-maven-3.8.4 Java version: 17.0.2, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64 Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "5.13.0-39-generic", arch: "amd64", family: "unix"

Additional information

No response

@ivansenic ivansenic added the kind/bug Something isn't working label Apr 13, 2022
@quarkus-bot
Copy link

quarkus-bot bot commented Apr 14, 2022

/cc @geoand

@famod
Copy link
Member

famod commented Apr 14, 2022

There is a special quarkus-junit5-mockito dependency you'll have to use, which is providing special annotations like @InjectMock. See e.g. https://quarkus.io/guides/getting-started-testing#further-simplification-with-injectmock

I don't think @Captor will work with this, though.

@geoand
Copy link
Contributor

geoand commented Apr 14, 2022

This is an inherent limitation of how we handle tests - we essentially need to create a proxy of the test class.
This won't be changed any time soon.

FWIW, you can use @InjectMock for dealing with mocks.
If you need additional support around mocks, feel free to open an enhancement issue.

@geoand geoand closed this as completed Apr 14, 2022
@geoand geoand added the triage/wontfix This will not be worked on label Apr 14, 2022
@ivansenic
Copy link
Contributor Author

Would @InjectMocks also work for the mocks that are actually not beans? I mean let's say I want to mock a class that is not a bean and not in the CDI context, I doubt @InjectMocks would work.

Also this means any Mockito extension functionality, as @Captor can not be used. Which kind of makes me think about the other Junit extensions out there. Are you actually saying we have limitations on how we handle tests and yes you can not use any of the extensions out there? Is this really the good approach?

@geoand
Copy link
Contributor

geoand commented Apr 14, 2022

It only works for beans

@geoand
Copy link
Contributor

geoand commented Apr 14, 2022

There are various limitations, yes.

I never said it was a good approach, it was however the only one that allowed us to support the feature set we need.

If JUnit supports loading test classes from a custon ClassLoader in the future (there is an open issue), we could ditch this approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/testing kind/bug Something isn't working triage/wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants