You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When including the quarkus-junit5-mockito dependency, Mockito fails to mock final classes, which is a problem especially for Kotlin, since all classes there are final if not explicitly declared open. This behavior is different from the one you get when you just include mockito-core in version 5.12.0, which is the same version Quarkus seems to use internally.
Even if you declare the class as open, the mock will behave differently to plain Mockito. For example, with plain Mockito, if you set a property of the mocked class, this will show as a setter invocation when using the Mockito.mockingDetails method. However, when using the Quarkus dependency, no such invocation is shown.
Cannot mock/spy class org.acme.TestClass
Mockito cannot mock/spy because :
- final class
Even after adding an open in front of the TestClass, it still fails to show the setting of the property as a method invocation and the assertion fails.
You can switch the dependencies in the pom.xml and add the open keyword in front of the TestClass and execute the tests to see the different behaviors.
Output of uname -a or ver
Linux some-Laptop 6.5.0-45-generic #45~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Jul 15 16:40:02 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Output of java -version
openjdk version "21.0.2" 2024-01-16 LTS
Quarkus version or git rev
3.14
Build tool (ie. output of mvnw --version or gradlew --version)
Apache Maven 3.9.8
Additional information
No response
The text was updated successfully, but these errors were encountered:
I figured it out and it is actually intended behavior. Here is my solution for anyone that might stumble on the problem with Kotlin that I described:
Quarkus uses the "subclass" variant of Mockito by default instead of the "inline" variant, which is Mockito's default. To get Mockito's default behavior, just follow this guide if you use Maven or add the following in your build.gradle.kts if you use Gradle:
Describe the bug
When including the
quarkus-junit5-mockito
dependency, Mockito fails to mock final classes, which is a problem especially for Kotlin, since all classes there are final if not explicitly declaredopen
. This behavior is different from the one you get when you just includemockito-core
in version5.12.0
, which is the same version Quarkus seems to use internally.Even if you declare the class as open, the mock will behave differently to plain Mockito. For example, with plain Mockito, if you set a property of the mocked class, this will show as a setter invocation when using the
Mockito.mockingDetails
method. However, when using the Quarkus dependency, no such invocation is shown.Expected behavior
Given the following Kotlin class:
and the following test method:
I would expect the test to complete successfully.
Actual behavior
The
Mockito.mock
call fails:Even after adding an
open
in front of theTestClass
, it still fails to show the setting of the property as a method invocation and the assertion fails.How to Reproduce?
reproducer.zip
You can switch the dependencies in the
pom.xml
and add theopen
keyword in front of theTestClass
and execute the tests to see the different behaviors.Output of
uname -a
orver
Linux some-Laptop 6.5.0-45-generic #45~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Jul 15 16:40:02 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Output of
java -version
openjdk version "21.0.2" 2024-01-16 LTS
Quarkus version or git rev
3.14
Build tool (ie. output of
mvnw --version
orgradlew --version
)Apache Maven 3.9.8
Additional information
No response
The text was updated successfully, but these errors were encountered: