Mockito 2 message - "Mockito can only mock non-private & non-final classes." correct

5,182 views
Skip to first unread message

mikael petterson

unread,
Jul 4, 2018, 4:06:18 AM7/4/18
to mockito
Hi,

Wee have upgraded to Mockito 2 (2.8.9 since it worked fine with PowerMock).

When converting tests we got the following message.

I thought mocking final classes and methods was a new feature in Mockito 2.

"Mockito can only mock non-private & non-final classes."

The above contradicts it. Or will the message go away when we enable it using:


What do you think?

//mike


Complete message:
===============

org.mockito.exceptions.base.MockitoException:

Mockito cannot mock this class: interface com.company.mobjects.ObjectHandler.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.


Java               : 1.8
JVM vendor name    : Oracle Corporation
JVM vendor version : 25.0-b62
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 1.8.0-ea-b120
JVM info           : mixed mode
OS name            : Linux
OS version         : 2.6.32-504.el6.x86_64


Underlying exception : java.lang.IllegalArgumentException: object is not an instance of declaring class
    at com.company.DotImplTest.setup(DotImplTest.java:31)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at com.company.DotImplTest.setup(DotImplTest.java:31)

We are using an old version of java:https://stackoverflow.com/questions/37527038/mockito-object-is-not-an-instance-of-declaring-class

Use at least jdk 1.8_172

Tim van der Lippe

unread,
Jul 4, 2018, 4:17:58 AM7/4/18
to moc...@googlegroups.com
Your JDK version is quite old. Please update to a more recent version that does work.

--
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/ae51b44a-ccb0-41f9-946a-04b2e18ddb37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mikael petterson

unread,
Jul 4, 2018, 4:33:27 AM7/4/18
to mockito
Yes I did and problem disappeared. My question was about the message that mockito 2 spits out.
Since mockito 2 can mock final classes :-)

//mikael

Tim van der Lippe

unread,
Jul 4, 2018, 5:33:44 AM7/4/18
to moc...@googlegroups.com
Ah I see. Yeah, at that point Mockito does not know what is broken, just that it is broken. Doubt we can improve the message :(

mikael petterson

unread,
Jul 5, 2018, 3:22:57 AM7/5/18
to mockito
Yes but is it not possible to just remove:
& non-final classes.

since the statement is not valid in mockito 2. It is confusing for mockito 2 users.

//mikael
Reply all
Reply to author
Forward
0 new messages