Cannot mock/spy because : final class ... gradle only

Skip to first unread message

Jean Helou

May 6, 2022, 11:20:23 AM5/6/22
to mockito
Hello everyone,

I am trying to figure out exactly how mockito selects its mock maker:
I participate in an effort to migrate a very large maven build from maven to gradle.

An issue  popped in the migration effort, in the gradle build we seem to be unable to spy final classes even though the very same tests compiled with maven work just fine.

I assume it is a dependency resolution issue of some kind but I have been unable to pinpoint it for now, we have a pretty large classpath so identifying relevant libs is not trivial. I couldn't find  a way to debug the mockmaker selection in the mockito FAQ  or documentation and stack overflow didn't yield anything interesting (or I failed to recognize it)

Here is what I am certain of at the moment:
- Both builds were run on JDK 11
- Both builds use the following exact dependencies during runtime:
  - mockito 3.12.4
  - byte buddy 1.9.16
  - byte byddy agent 1.11.22
  - objenesis 3.2
  - mockito-junit-jupiter 3.12.4
  - junit-jupiter-api 5.8.2

Neither build declares a dependency on mockito-inline which I have seen mentionned multiple times when researching the error message. It is unclear to me if the mockito inline dependency is for mockito 2 only or if it still applies to mockito 3+,
I assume that since the test works with maven, the dependency is not actually required for our use case.

the spy error on gradle popped for 2 different classes :
- one java class which is explicitly marked final
- one kotlin class which is implicitely final (since it is not marked open)

Any pointers on next steps to understand what's going on will be very appreciated

Tim van der Lippe

May 6, 2022, 3:39:27 PM5/6/22
to mockito
You should use `mockito-inline` if you want to spy final classes, so I recommend including it. We will make the inline mockmaker the default in the next major version of Mockito, so it is safe to adopt the new artifact ahead of time.

You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Jean Helou

May 10, 2022, 2:28:40 AM5/10/22
to mockito
Thanks again, adding the dependency to both builds fixed the gradle tests and had no adverse effects on the maven tests

Reply all
Reply to author
0 new messages