Tôi chưa chạm vào Mockito một thời gian và tôi chưa bao giờ sử dụng nó rộng rãi. Nhưng hôm nay tại một số mã mới, tôi muốn sử dụng thực hành tốt nhất vì vậy tôi kéo ra Mockito lõi 2.0.54-beta sử dụng phiên bản Java sau:Đối tượng Mockito không phải là trường hợp khai báo lớp
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)
tôi cần một thể hiện của giao diện này để thử nghiệm:
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;
}
giao diện đó mở rộng này (chỉ cần một giao diện đánh dấu trống):
Trong thử nghiệm của tôi, tôi đã cố gắng để làm điều đơn giản này:
final ResourceI18nConcern defaultConcern=mock(ResourceI18nConcern.class);
tôi nhận được stack trace sau:
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
Am tôi làm sai điều gì? Điều này dường như với tôi như thể nó nên được đơn giản.
Để tái tạo sự cố này, hãy sao chép https://bitbucket.org/globalmentor/rincl.git và kiểm tra cam kết 2f88d7c5e5ac17b6d316ed54c12cb7b447b7d6ac. Sau đó, hãy thử chạy thử nghiệm sau:
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);
}
}
Không đủ thông tin: Phiên bản Java, thử nghiệm thực tế, OSGI, v.v.? Tôi không có bất kỳ vấn đề nhạo báng mã được đề cập ở trên. Đối với tôi, điều này có vẻ là vấn đề về tải lớp có thể do thiết lập môi trường gây ra. – Brice
Tuy nhiên với một cái nhìn thứ hai tại stacktrace này có thể looke như một vấn đề một vấn đề classloading gây ra bởi các thiết lập môi trường Android. Điều này có thể là một cái gì đó mà bytebuddy có thể đối phó với tuy nhiên. – Brice
OK, @Brice, tôi đã thêm một cam kết cụ thể để bạn kiểm tra và kiểm tra mã cụ thể. –