Mockito 2 final class mocking limitations

2,015 views
Skip to first unread message

Andrew Audibert

unread,
Oct 27, 2016, 5:01:54 AM10/27/16
to mockito
Hi all,

Our team has run into many issues with PowerMock due to its replacing the system classloader. I'm excited to see that core Mockito is coming out with its own way of mocking final classes. Does the Mockito approach also rely on modifying the classloader? The 2.1 announcement mentions

Mocking of final classes and methods is an incubating, opt-in feature. It uses a combination of Java agent instrumentation and subclassing in order to enable mockability of these types. As this works differently to our current mechanism and this one has different limitations and as we want to gather experience and user feedback, this feature had to be explicitly activated to be available

Can anyone expand on what these different limitations are? 

Thanks,
Andrew Audibert

Tim van der Lippe

unread,
Oct 27, 2016, 5:06:45 AM10/27/16
to mockito
Hello Andrew,

While the Javadoc is not available on the site, we listed the limitations in the Javadoc of the new MockMaker: https://github.com/mockito/mockito/blob/release/2.x/src/main/java/org/mockito/internal/creation/bytebuddy/InlineByteBuddyMockMaker.java#L67-L73 

You can not mock native methods and can no longer strip synchronized modifiers from mocked instances. Package-visible methods will not be mocked by the new mockmaker.

Hope that answers your question.

Enjoy mocking with Mockito 2!

Tim

Op do 27 okt. 2016 om 11:01 schreef Andrew Audibert <aaudi...@gmail.com>:
--
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 mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/66ac814e-d78b-457a-91a5-1bca281cd8c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brice Dutheil

unread,
Oct 27, 2016, 5:15:35 AM10/27/16
to mockito
Hi Andrew,
Thanks for the Mockito final class excitement :) Keep in mind this is still experimental.
Powermock used clever technics with the classloader and plugged in the internals of mockito, however they said they are not enough contributors and that they have not enough time to make Powermock compatible with mockito 2 at this time.
On a project with my client I completely removed Powermock.
As you have read below mockito rely on instrumentation. That means that mockito 2 does not need to play with classloader at all.
Quoted here :

Some noteworthy notes about this mock maker:

  • Mocking final types and enums is incompatible with mock settings like :
    • explicitly serialization support withSettings().serializable()
    • extra-interfaces withSettings().extraInterfaces()
  • Some methods cannot be mocked
    • Package-visible methods of java.*
    • native methods
  • This mock maker has been designed around Java Agent runtime attachment ; this require a compatible JVM, that is part of the JDK (or Java 9 VM). When running on a non-JDK VM prior to Java 9, it is however possible to manually add the Byte Buddy Java agent jar using the -javaagentparameter upon starting the JVM.

-- Brice

_____________________________
From: Andrew Audibert <aaudi...@gmail.com>
Sent: Thursday, October 27, 2016 11:01
Subject: [mockito] Mockito 2 final class mocking limitations
To: mockito <moc...@googlegroups.com>
--

Brice Dutheil

unread,
Oct 27, 2016, 5:16:22 AM10/27/16
to mockito
Seems that Tim raced me ;)

Bartosz Miller

unread,
Nov 2, 2016, 4:28:21 AM11/2/16
to mockito
Hi Tim,

I started to play with mocking final classes, tried to mock String.class and ... fail! 

I noticed in the source that the following list of classes in excluded from mocking: 
Class.class, Boolean.class, Byte.class, Short.class, Character.class, Integer.class, Long.class, Float.class, Double.class, String.class

What is the technical reason of that?

Regards,
Bartosz

Brice Dutheil

unread,
Nov 2, 2016, 5:30:20 AM11/2/16
to moc...@googlegroups.com
They have special handling in the JDK itself. Messing with those break the JDK.

-- Brice

To unsubscribe from this group and stop receiving emails from it, send an email to mockito+unsubscribe@googlegroups.com.

To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.

David Karr

unread,
Nov 2, 2016, 12:02:25 PM11/2/16
to moc...@googlegroups.com
What is your technical reason for wanting to mock String?
> https://groups.google.com/d/msgid/mockito/9565ccd9-bffe-4d35-a9a4-491fe2dec12e%40googlegroups.com.

Brice Dutheil

unread,
Nov 2, 2016, 12:33:18 PM11/2/16
to moc...@googlegroups.com
The same, it breaks your JDK runtime.

-- Brice


> To post to this group, send email to moc...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mockito.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mockito/9565ccd9-bffe-4d35-a9a4-491fe2dec12e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
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 mockito+unsubscribe@googlegroups.com.

To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.

David Karr

unread,
Nov 2, 2016, 3:22:10 PM11/2/16
to moc...@googlegroups.com
To be clear, I'm asking the OP why he's even attempting to mock
String. That could short-circuit these attempts to mock things that
are unmockable.
>> > https://groups.google.com/d/msgid/mockito/9565ccd9-bffe-4d35-a9a4-491fe2dec12e%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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 mockito+u...@googlegroups.com.
>> To post to this group, send email to moc...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/mockito.
>> To view this discussion on the web visit
> --
> 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 mockito+u...@googlegroups.com.
> To post to this group, send email to moc...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mockito.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mockito/CALTMA7u1UDksxLC4WZ0aiV%2B1BkikSs-f_kH%3DB_-jGKrHjkLUZw%40mail.gmail.com.

Marcin Grzejszczak

unread,
Nov 4, 2016, 3:20:05 AM11/4/16
to mockito
Hi Bartosz,

can you show a sample of your code so that we can understand what you want to do? What you're doing sounds like a terrible antipatern.
Reply all
Reply to author
Forward
0 new messages