我已经有一段时间没有接触过Mockito了,我也从未广泛使用它。但是今天,在一些新代码中,我想使用最佳实践,因此我使用以下Java版本退出了模仿内核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)
我需要此接口的实例进行测试:
https://bitbucket.org/globalmentor/rincl/src/bf0e8875a1bae16247dd904e0b8bc067c9f8abc9/src/main/java/io/rincl/ResourceI18nConcern.java
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;
}
该接口扩展了这个接口(只是一个空的标记接口):
https://bitbucket.org/globalmentor/csar/src/125b837e9936ddb8d6340fa99b6b5da872d1e6e5/src/main/java/io/csar/Concern.java
在测试中,我尝试执行以下简单操作:
final ResourceI18nConcern defaultConcern=mock(ResourceI18nConcern.class);
我得到以下堆栈跟踪:
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
难道我做错了什么?在我看来,这似乎应该很简单。
要重现此问题,请克隆https://bitbucket.org/globalmentor/rincl.git并签出commit
2f88d7c5e5ac17b6d316ed54c12cb7b447b7d6ac。然后尝试运行以下测试:
import 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);
}
}
您的Java运行时版本从2014年3月开始;此后,VM中已修复了许多错误,您应该进行真正的升级。我99%确信此问题与@NonNull
该版本中首次在此版本中首次引入的类型注释()有关。我相信,如果升级虚拟机,您的问题将会消失。
我可以使用最新版本的HotSpot VM成功执行您建议的测试。
我有一段时间没有接触过Mockito,也从来没有大量使用过它。但是今天在一些新代码中,我希望使用最佳实践,所以我使用以下Java版本推出了mockito-core 2.0.54-beta: 要重现此问题,请克隆https://bitbucket.org/globalmentor/rincl.git并签出commit2f88d7c5e5ac17b6d316ed54c12cb7b447b7d6ac。然
问题内容: 这个问题在Google上到处都有,但我仍然遇到问题。这就是我想要做的。因此,就像标题状态一样,我遇到了“对象不是声明类的实例”错误。有任何想法吗?谢谢! Main.java TestFunction.java 问题答案: 您正在使用该类调用该方法,但需要一个实例。试试这个:
我已经设置了一个Spring Boot项目,并使用castor xml依赖关系将POJO转换为xml。 在为POJO调用封送处理方法时遇到问题。我查阅了stackoverflow的其他相关问题,但没有一个能解决我的场景。 有一个toString方法,它位于Spring组件中。 以下是片段: 这是客户端内部的封送方法(castor之前自动生成的类): 以下是相同的堆栈跟踪: 这里需要注意的是,当我执
用户类 我把这个复制方法叫做 并在执行writeMethod.invoke(dest,val)时获得以下异常。有人能帮忙吗? 对象不是在java.base/jdk.internal.reflect.nativeMethodAccessorImpl.Invoke0(原生方法)在java.base/jdk.internal.reflect.nativeMethodAccessorImpl.Invoke
我有一个CookingEvent.class,它是Event.class的子类,hibernate继承策略是@ Inheritance(strategy = Inheritance type。已加入)。当我试图发送一个列表对象作为get响应时。我不例外 2016-08-25 11:49:22.351错误11944---[nio-8189-exec-1]o.a.c.c.c.[.[/]。[servle
问题内容: 我的ejb项目使用的是Hibernate 4.3.6和Glassfish 4.0。 我的考试道课: 和POJO类Foo.class: persistence.xml: 我可以列出表数据,通过查询获取数据,也可以从表中删除数据。但是我不能坚持或合并。 例外是: 我在哪里做错了? 此项目在具有persistence.xml版本1.0(jpa)且没有此行的Glassfish 3.1上工作: