Error reported by Mockito v2.7.11 but not by v1.10.19

633 views
Skip to first unread message

Manchun Zheng

unread,
Feb 24, 2017, 1:54:07 PM2/24/17
to mockito
Hi,

I'm trying to use Mockito to mock a public interface defined by vertx-web in my unit test.
The interface I want to mock is io.vertx.ext.web.RoutingContext and below is the simple test that I write to demonstrate this problem.

package com.mc.test.TestMockito;


import static org.junit.Assert.assertTrue;

import static org.mockito.Mockito.mock;


import org.junit.Test;


import io.vertx.ext.web.RoutingContext;


public class MockRtxTest

{

    @Test

    public void mockRoutingContext()

    {

        RoutingContext rtx = mock( RoutingContext.class );


        assertTrue( rtx != null );

    }

}


Here is the dependencies in my pom.xml. In this configuration, I Mockito v1.10.19 is used so the test passes successfully. 

         <dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>io.vertx</groupId>

<artifactId>vertx-web</artifactId>

<version>3.3.3</version>

</dependency>

<dependency>

<groupId>org.mockito</groupId>

<artifactId>mockito-all</artifactId>

<version>1.10.19</version>

<scope>test</scope>

</dependency>

</dependencies>



However, if I switched to Mockito 2.7.11, as shown in the following,

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>io.vertx</groupId>

<artifactId>vertx-web</artifactId>

<version>3.3.3</version>

</dependency>

<dependency>

<groupId>org.mockito</groupId>

<artifactId>mockito-core</artifactId>

<version>2.7.11</version>

<scope>test</scope>

</dependency>

</dependencies>


the following error is reported:

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: interface io.vertx.ext.web.RoutingContext.

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.121-b13
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 1.8.0_121-b13
JVM info           : mixed mode
OS name            : Mac OS X
OS version         : 10.12.3


Underlying exception : java.lang.IllegalStateException: Error invoking java.lang.reflect.Method#getAnnotatedReturnType
at com.mc.test.TestMockito.MockRtxTest.mockRoutingContext(MockRtxTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: Error invoking java.lang.reflect.Method#getAnnotatedReturnType
at net.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$Dispatcher$ForJava8CapableVm$AnnotatedReturnType.resolve(TypeDescription.java:2660)
at net.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$Delegator.asList(TypeDescription.java:2926)
at net.bytebuddy.description.type.TypeDescription$Generic$OfNonGenericType$ForLoadedType.getDeclaredAnnotations(TypeDescription.java:3497)
at net.bytebuddy.implementation.attribute.AnnotationAppender$ForTypeAnnotations.apply(AnnotationAppender.java:639)
at net.bytebuddy.implementation.attribute.AnnotationAppender$ForTypeAnnotations.onNonGenericType(AnnotationAppender.java:620)
at net.bytebuddy.implementation.attribute.AnnotationAppender$ForTypeAnnotations.onNonGenericType(AnnotationAppender.java:333)
at net.bytebuddy.description.type.TypeDescription$Generic$OfNonGenericType.accept(TypeDescription.java:3364)
at net.bytebuddy.implementation.attribute.MethodAttributeAppender$ForInstrumentedMethod.apply(MethodAttributeAppender.java:171)
at net.bytebuddy.implementation.attribute.MethodAttributeAppender$Compound.apply(MethodAttributeAppender.java:425)
at net.bytebuddy.dynamic.scaffold.TypeWriter$MethodPool$Record$ForDefinedMethod$WithBody.applyAttributes(TypeWriter.java:609)
at net.bytebuddy.dynamic.scaffold.TypeWriter$MethodPool$Record$ForDefinedMethod$WithBody.applyBody(TypeWriter.java:601)
at net.bytebuddy.dynamic.scaffold.TypeWriter$MethodPool$Record$ForDefinedMethod.apply(TypeWriter.java:521)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForCreation.create(TypeWriter.java:4089)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default.make(TypeWriter.java:1612)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:174)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:155)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:2560)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$Delegator.make(DynamicType.java:2662)
at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:94)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:37)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:34)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:32)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:71)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:42)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:26)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:65)
at org.mockito.Mockito.mock(Mockito.java:1702)
at org.mockito.Mockito.mock(Mockito.java:1615)
... 24 more
Caused by: java.lang.NullPointerException
at sun.reflect.annotation.TypeAnnotationParser.mapTypeAnnotations(TypeAnnotationParser.java:356)
at sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl.<init>(AnnotatedTypeFactory.java:139)
at sun.reflect.annotation.AnnotatedTypeFactory.buildAnnotatedType(AnnotatedTypeFactory.java:65)
at sun.reflect.annotation.TypeAnnotationParser.buildAnnotatedType(TypeAnnotationParser.java:79)
at java.lang.reflect.Executable.getAnnotatedReturnType0(Executable.java:633)
at java.lang.reflect.Method.getAnnotatedReturnType(Method.java:648)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at net.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$Dispatcher$ForJava8CapableVm$AnnotatedReturnType.resolve(TypeDescription.java:2656)
... 56 more

I don't see why it becomes a problem in v2.7.11 while it is totally OK with v1.10.19. So I'm guessing that this might be a bug of v2.7.11.
Or maybe something is changed in v2.7.11 and I didn't use it appropriately.
I would appreciate it if someone could kindly give me some insight regarding this.

Thanks & Best Regards,
Manchun

Allon Mureinik

unread,
Feb 24, 2017, 2:27:53 PM2/24/17
to moc...@googlegroups.com
I played around with this a bit, and can confirm this happens with any Mockito 2.x version I tested all the way back to 2.1.0.

--
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/f225d962-ce97-48e9-91ce-dec372b443a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Schwarz

unread,
Apr 6, 2017, 4:35:27 AM4/6/17
to mockito
The mock creator changed from cglib in mockito 1 to byte buddy in mockito 2. The root cause (NPE) points to byte buddy code. It seems to be a byte buddy bug, has this be reported on github ?

Maurice de Chateau

unread,
Jan 24, 2018, 10:05:00 AM1/24/18
to mockito
Perhaps a little late for the starter of this topic, but I encountered the very same problem (with vert.x 3.3.3 and Mockito 2.x.x), and found the following issue on the Mockito Github:


Two possible work-arounds can be derived from that thread:
  • include io.vertx:vertx-codegen in your Maven/Gradle/... build file as a test dependency (this worked for me)
  • upgrade to a Java 9 build in which the underlying JDK-issue is resolved (not sure as of which build number that is, but since the fix was added in july of 2016, a recent one should be OK)
Hope this helps anyone who finds this thread upon encountering the same problem...

Best regards,
Maurice de Chateau
Reply all
Reply to author
Forward
0 new messages