Open avenue68 opened 1 month ago
A @SpringBean
gets automatically the marker interface org.spockframework.spring.mock.SpockSpringProxy
added to the mock instance, in the class SpockSpringProxyCreator
.
But final classes with additonal interfaces are not mockable (also not by mockito itself) due to technical limitations.
See also error message in the exception: "mockito: Cannot mock final classes with additional interfaces."
So as you already mentioned as a workaround remove the final
.
@leonard84 Maybe we could remove the automatical addition of the SpockSpringProxy
marker interface.
Do you know why this interface was introduced, I don't find production code where a mock instance is checked against. I find only checks in tests.
Do you think we could remove the marker interface all together?
@AndreasTu Thank you for checking. By the way what does "additional" interface mean? Just implemented interfaces?
You can specify that a mock should implement additional Interfaces in addition to the mock class. So you could create a mock instance of YourClass
and also implement e.g. Runnable
and the later cast the mock instance to Runnable
.
This is done by internally subclassing YourClass
and implementing the additional interfaces automatically. That is also the reason why final classes do not work, because we can't subclass a final class in the JVM.
Describe the bug
My spock test results in an exception occurrence when I try to mock a final class which is registered as a Spring bean. I have a mockito dependency in the classpath and enabled it by
SpockConfiguration.groovy
.And this problem not occur when the mocked class is non-final or Spring is not used.
To Reproduce
The code bellow reproduces the problem.
Expected behavior
Exception not thrown and can mock final classes properly.
Actual behavior
CannotCreateMockException
is thrown.The stacktrace is bellow.
Java version
openjdk version "17.0.10" 2024-01-16 LTS OpenJDK Runtime Environment Corretto-17.0.10.7.1 (build 17.0.10+7-LTS) OpenJDK 64-Bit Server VM Corretto-17.0.10.7.1 (build 17.0.10+7-LTS, mixed mode, sharing)
Buildtool version
Gradle 8.4
Build time: 2023-10-04 20:52:13 UTC Revision: e9251e572c9bd1d01e503a0dfdf43aedaeecdc3f
Kotlin: 1.9.10 Groovy: 3.0.17 Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023 JVM: 17.0.10 (Amazon.com Inc. 17.0.10+7-LTS) OS: Mac OS X 13.5 aarch64
What operating system are you using
Mac
Additional context
No response