java Mockito 对象不是声明类的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37527038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito object is not an instance of declaring class
提问by Garret Wilson
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 using the following Java version:
我有一段时间没有接触 Mockito,也从未广泛使用它。但是今天在一些新代码中,我想使用最佳实践,所以我使用以下 Java 版本提取了 mockito-core 2.0.54-beta:
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
I needed an instance of this interface for testing:
我需要这个接口的一个实例来进行测试:
public interface ResourceI18nConcern extends Concern {
public Locale getLocale(@Nonnull Locale.Category category);
public void setLocale(@Nonnull Locale.Category category, @Nonnull Locale locale);
public default void setLocale(@Nonnull Locale locale) {...}
public Resources getResources(@Nonnull final Class<?> contextClass) throws ResourceConfigurationException;
}
That interface extends this one (just an empty marker interface):
该接口扩展了这个接口(只是一个空的标记接口):
In my test I tried to do this simple thing:
在我的测试中,我尝试做这个简单的事情:
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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.accessimport static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.Locale;
import java.util.Locale.Category;
import org.junit.*;
public class RinclTest {
@Test
public void defaultConcernRinclGetLocaleIsConcernLocale() {
final ResourceI18nConcern defaultConcern=mock(ResourceI18nConcern.class);
}
}
0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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
Am I doing something wrong? This seemed to me as if it should be straightforward.
难道我做错了什么?在我看来,这应该是直截了当的。
To reproduce this problem, clone https://bitbucket.org/globalmentor/rincl.gitand check out commit 2f88d7c5e5ac17b6d316ed54c12cb7b447b7d6ac. Then try to run the following test:
要重现此问题,请克隆https://bitbucket.org/globalmentor/rincl.git并检查提交 2f88d7c5e5ac17b6d316ed54c12cb7b447b7d6ac。然后尝试运行以下测试:
##代码##回答by Rafael Winterhalter
Your Java runtime version is from March 2014; plenty of bugs have been fixed in the VM ever since and you should really upgrade. I am 99% sure that this problem is related to type annotations (@NonNull
) which were introduced in this version for the first time in this exact release. I am sure that your problem will go away if you upgrade your VM.
您的 Java 运行时版本是 2014 年 3 月的;从那时起,VM 中已经修复了许多错误,您应该真正升级。我 99% 确定此问题与@NonNull
此版本中首次在此确切版本中引入的类型注释 ( )有关。我相信如果你升级你的虚拟机,你的问题就会消失。
I can successfully execute your proposed test with a recent build of the HotSpot VM.
我可以使用 HotSpot VM 的最新版本成功执行您提议的测试。
回答by dqromney
Had the same issue with my project and used the Rincl project above to confirm I was having the same issue. Using JDK 1.8.0_11 on Mac the reported errors were the same as Garret. Updating to version JDK 1.8.0_181 I ran the Rincl test with NO errors. Apparently Mockito is using the latest of the JDK 1.8.0.
我的项目有同样的问题,并使用上面的 Rincl 项目来确认我有同样的问题。在 Mac 上使用 JDK 1.8.0_11 报告的错误与 Garret 相同。更新到版本 JDK 1.8.0_181 我运行 Rincl 测试没有错误。显然 Mockito 使用的是最新的 JDK 1.8.0。
回答by Divas
If this is helpful to anyone, I got the same above issue and tried all other options like upgrading the JVM etc, but what solved my issue is : https://github.com/mockito/mockito/issues/1606
如果这对任何人都有帮助,我遇到了上述相同的问题并尝试了所有其他选项,例如升级 JVM 等,但解决我的问题的是:https: //github.com/mockito/mockito/issues/1606
By and large reverting my mockito version to 2.23.+ solved the issue for me.
总的来说,将我的 mockito 版本恢复到 2.23.+ 为我解决了这个问题。