Creating a mock of a public non-final class (org.kohsuke.github.GHIssue) fails with "Cannot cast to primitive type: void"

72 views
Skip to first unread message

Adam Siemion

unread,
Oct 18, 2016, 5:40:32 AM10/18/16
to mockito
Hi guys, a super simple test that creates a new mock of a public non-final class org.kohsuke.github.GHIssue from https://github.com/kohsuke/github-api version 1.77, source https://github.com/kohsuke/github-api/blob/github-api-1.77/src/main/java/org/kohsuke/github/GHIssue.java fails with:

Caused by: java.lang.IllegalArgumentException: Cannot cast to primitive type: void

I think this is a bug, correct me if I am wrong.

BTW. I have created a simple project exposing this issue: https://github.com/adamsiemion/mockito-bug

Thanks,
Adam

Adam Siemion

unread,
Oct 18, 2016, 6:08:28 AM10/18/16
to mockito
I have came across another issue, creating a new mock of another public non-final class GHRepository results in:

java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    org/kohsuke/github/GHRepository$MockitoMock$667330966.getId()Ljava/lang/String; @4: checkcast
  Reason:
    Type integer (current frame, stack[0]) is not assignable to 'java/lang/Object'
  Current Frame:
    bci: @4
    flags: { }
    locals: { 'org/kohsuke/github/GHRepository$MockitoMock$667330966' }
    stack: { integer }
  Bytecode:
    0x0000000: 2ab6 003d c000 2eb0                    

at sun.reflect.GeneratedSerializationConstructorAccessor1.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:45)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)

Thanks,
Adam

Brice Dutheil

unread,
Oct 18, 2016, 8:40:26 AM10/18/16
to moc...@googlegroups.com

@Adam Thanks for the report !!

I’ve looked and reproduced the issue you are seeing with mockito 2.2.3 (bb 1.4.26) and 2.2.5 (bb 1.4.33). Regardless of the JDK.It seems related to how kohsuke/github-api generates byte code for backward compatibility, this is highly unusual and may produce valid bytecode but unmet bytecode patterns for mockito / bytebuddy.
For reference the tool is called bridge-method-injector, more on the website

The classes you are mentioning are modified by this tool :

  • GHIssues annotated by @WithBridgeMethods(void.class)
  • GHRepository that inherits GHObject which also annotated by @WithBridgeMethods(value=String.class, adapterMethod="intToString")

It works with 1.10.19, because CGLIB is far less intelligent regarding bridge methods.

At the moment mocking these classes is not yet possible with mockito 2. I will fill an issue on mockito project.

— Brice

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/951298fb-3532-4b37-8553-5051c2edc32d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Szczepan Faber

unread,
Oct 18, 2016, 9:38:04 AM10/18/16
to moc...@googlegroups.com
Adam, thank you very much for reporting!!!

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.

--
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.

For more options, visit https://groups.google.com/d/optout.
--
Szczepan Faber
Founder @ mockito.org | Twitter @ szczepiq

Brice Dutheil

unread,
Oct 18, 2016, 1:22:01 PM10/18/16
to moc...@googlegroups.com
@Adam, thanks again for the great report ! I filled the issue here : https://github.com/mockito/mockito/issues/701

-- 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.

--
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.
--
Szczepan Faber
Founder @ mockito.org | Twitter @ szczepiq

--
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.

Adam Siemion

unread,
Nov 7, 2016, 5:53:34 AM11/7/16
to mockito
Guys, I am amazed by your prompt response (it took you a few hours to reply and while it took me 3 weeks to reply :)).
You have great community! 
I am glad I could help and would love to do that again in the future. 

All the best, Thanks, 
Adam

-- Brice

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.

--
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.
--
Szczepan Faber
Founder @ mockito.org | Twitter @ szczepiq

--
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.

Brice Dutheil

unread,
Nov 7, 2016, 6:24:17 AM11/7/16
to moc...@googlegroups.com
Hi Adam,

Thanks for these kind words ;)
By the way the fix is done (since 2.2.9 or later), thanks to Rafael !

-- 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.
Reply all
Reply to author
Forward
0 new messages