object is not an instance of declaring class

1,826 views
Skip to first unread message

Garret Wilson

unread,
May 29, 2016, 6:05:27 AM5/29/16
to mockito
I haven't touched Mockito for a while, and I have never used it extensively. But today in some new code I wanted to use best practices so I pulled out mockito-core 2.0.54-beta.

I needed an instance of this interface:

https://bitbucket.org/globalmentor/rincl/src/bf0e8875a1bae16247dd904e0b8bc067c9f8abc9/src/main/java/io/rincl/ResourceI18nConcern.java

That interface extends this one (just an empty marker interface):

https://bitbucket.org/globalmentor/csar/src/125b837e9936ddb8d6340fa99b6b5da872d1e6e5/src/main/java/io/csar/Concern.java

I tried to do a simple:

final ResourceI18nConcern defaultConcern=mock(ResourceI18nConcern.class);

I get the following stack trace:

org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: interface io.rincl.ResourceI18nConcern

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

Underlying exception : java.lang.IllegalArgumentException: object is not an instance of declaring class
    at io
.rincl.RinclTest.defaultConcernRinclGetLocaleIsConcernLocale(RinclTest.java:91)
    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:483)
    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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org
.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    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:675)
    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.IllegalArgumentException: object is not an instance of declaring class
    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:483)
    at net
.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$ForTypeArgument.resolve(TypeDescription.java:3875)
    at net
.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$Delegator$Chained.resolve(TypeDescription.java:3501)
    at net
.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$ForTypeArgument.resolve(TypeDescription.java:3834)
    at net
.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$Delegator.asList(TypeDescription.java:3477)
    at net
.bytebuddy.description.type.TypeDescription$Generic$OfWildcardType$ForLoadedType.getDeclaredAnnotations(TypeDescription.java:4771)
    at net
.bytebuddy.description.type.TypeDescription$Generic$Visitor$TypeVariableErasing.onWildcard(TypeDescription.java:676)
    at net
.bytebuddy.description.type.TypeDescription$Generic$Visitor$TypeVariableErasing.onWildcard(TypeDescription.java:659)
    at net
.bytebuddy.description.type.TypeDescription$Generic$OfWildcardType.accept(TypeDescription.java:4679)
    at net
.bytebuddy.description.type.TypeDescription$Generic$Visitor$TypeVariableErasing.onParameterizedType(TypeDescription.java:687)
    at net
.bytebuddy.description.type.TypeDescription$Generic$Visitor$TypeVariableErasing.onParameterizedType(TypeDescription.java:659)
    at net
.bytebuddy.description.type.TypeDescription$Generic$OfParameterizedType.accept(TypeDescription.java:5021)
    at net
.bytebuddy.description.type.TypeDescription$Generic$LazyProjection.accept(TypeDescription.java:5794)
    at net
.bytebuddy.description.method.ParameterDescription$TypeSubstituting.getType(ParameterDescription.java:866)
    at net
.bytebuddy.description.method.ParameterList$AbstractBase.asTypeList(ParameterList.java:85)
    at net
.bytebuddy.description.method.MethodDescription$AbstractBase.asTypeToken(MethodDescription.java:635)
    at net
.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default$Key$Harmonized.of(MethodGraph.java:862)
    at net
.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default$Key$Store.registerTopLevel(MethodGraph.java:1059)
    at net
.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.doAnalyze(MethodGraph.java:569)
    at net
.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.analyze(MethodGraph.java:529)
    at net
.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.doAnalyze(MethodGraph.java:565)
    at net
.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.compile(MethodGraph.java:502)
    at net
.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$AbstractBase.compile(MethodGraph.java:423)
    at net
.bytebuddy.dynamic.scaffold.MethodRegistry$Default.prepare(MethodRegistry.java:489)
    at net
.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:153)
    at net
.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$Delegator.make(DynamicType.java:2564)
    at org
.mockito.internal.creation.bytebuddy.MockBytecodeGenerator.generateMockClass(MockBytecodeGenerator.java:60)
    at org
.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.generate(CachingMockBytecodeGenerator.java:72)
    at org
.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.getOrGenerateMockClass(CachingMockBytecodeGenerator.java:64)
    at org
.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator.get(CachingMockBytecodeGenerator.java:27)
    at org
.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createProxyClass(ByteBuddyMockMaker.java:54)
    at org
.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:27)
    at org
.mockito.internal.util.MockUtil.createMock(MockUtil.java:32)
    at org
.mockito.internal.MockitoCore.mock(MockitoCore.java:54)
    at org
.mockito.Mockito.mock(Mockito.java:1443)
    at org
.mockito.Mockito.mock(Mockito.java:1356)
   
... 26 more




Garret Wilson

unread,
May 30, 2016, 9:36:05 AM5/30/16
to mockito
I have posted the question on StackOverflow as well.

Eric Lefevre-Ardant

unread,
May 30, 2016, 9:49:03 AM5/30/16
to moc...@googlegroups.com
It is possible you'll get an answer more switfly on Stackoverflow.

As for me, it feels that we are missing information, but I'm not sure what. Maybe posting a simplified version of the code could help.

On 30 May 2016 at 15:36, Garret Wilson <garret...@gmail.com> wrote:
I have posted the question on StackOverflow as well.

--
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/95e8b232-4711-43ce-ba16-f965400924a2%40googlegroups.com.

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

Brice Dutheil

unread,
May 30, 2016, 10:23:10 AM5/30/16
to moc...@googlegroups.com
@Rafael it seems there's some kind of issue when reading the class and creating the mock. Could be something wrong with the environment setup (classloading issue, the stacktrace isn't very clear about what it is but it seems to complain while processing)

-- Brice

Garret Wilson

unread,
May 30, 2016, 10:29:13 AM5/30/16
to moc...@googlegroups.com
I updated the StackOverflow question with a specific Git commit to check
out, along with specific test code.

Garret Wilson

unread,
May 30, 2016, 11:15:12 AM5/30/16
to mockito
It turns out that this apparently was being caused by a bug in an old version of the JVM. Switching to JDK 1.8.0_92 seems to have fixed the problem.
Reply all
Reply to author
Forward
0 new messages