我试图嘲笑一个公共课,但是当这样做时,Mockito会抛出Mockito cannot mock this class
例外。
测试代码:
package xyz.jacobclark.adapter;
import com.github.scribejava.apis.TwitterApi;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.oauth.OAuth10aService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.IOException;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsInstanceOf.any;
@RunWith(MockitoJUnitRunner.class)
public class TwitterOAuthAdapterTest {
@Mock
ServiceBuilder serviceBuilder;
@Test
public void getRequestTokenShouldReturnAValidRequestToken() throws IOException {
class TwitterOAuthAdapter {
private final OAuth10aService service;
public TwitterOAuthAdapter(ServiceBuilder serviceBuilder){
this.service = serviceBuilder
.apiKey("")
.apiSecret("")
.build(TwitterApi.instance());
}
public OAuth1RequestToken getRequestToken() throws IOException {
return this.service.getRequestToken();
}
}
TwitterOAuthAdapter oAuthAdapter = new TwitterOAuthAdapter(serviceBuilder);
Assert.assertThat(oAuthAdapter.getRequestToken(), is(any(OAuth1RequestToken.class)));
}
}
完整的堆栈跟踪:
xyz.jacobclark.adapter.TwitterOAuthAdapterTest
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.github.scribejava.core.builder.ServiceBuilder.
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.0-b70
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0-b132
JVM info : mixed mode
OS name : Mac OS X
OS version : 10.11.3
Underlying exception : java.lang.IllegalArgumentException: object is not an instance of declaring class
at org.mockito.internal.runners.SilentJUnitRunner$1.withBefores(SilentJUnitRunner.java:29)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:276)
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.mockito.internal.runners.SilentJUnitRunner.run(SilentJUnitRunner.java:39)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:103)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
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:3843)
at net.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$Delegator$Chained.resolve(TypeDescription.java:3468)
at net.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$ForTypeArgument.resolve(TypeDescription.java:3803)
at net.bytebuddy.description.type.TypeDescription$Generic$AnnotationReader$Delegator.asList(TypeDescription.java:3439)
at net.bytebuddy.description.type.TypeDescription$Generic$OfWildcardType$ForLoadedType.getDeclaredAnnotations(TypeDescription.java:4748)
at net.bytebuddy.description.type.TypeDescription$Generic$Visitor$TypeVariableErasing.onWildcard(TypeDescription.java:680)
at net.bytebuddy.description.type.TypeDescription$Generic$Visitor$TypeVariableErasing.onWildcard(TypeDescription.java:663)
at net.bytebuddy.description.type.TypeDescription$Generic$OfWildcardType.accept(TypeDescription.java:4656)
at net.bytebuddy.description.type.TypeDescription$Generic$Visitor$TypeVariableErasing.onParameterizedType(TypeDescription.java:691)
at net.bytebuddy.description.type.TypeDescription$Generic$Visitor$TypeVariableErasing.onParameterizedType(TypeDescription.java:663)
at net.bytebuddy.description.type.TypeDescription$Generic$OfParameterizedType.accept(TypeDescription.java:4998)
at net.bytebuddy.description.type.TypeList$Generic$AbstractBase.accept(TypeList.java:249)
at net.bytebuddy.description.type.TypeDescription$Generic$Visitor$TypeVariableErasing$RetainedTypeVariable.getUpperBounds(TypeDescription.java:832)
at net.bytebuddy.description.type.TypeDescription$Generic$OfTypeVariable.asErasure(TypeDescription.java:5373)
at net.bytebuddy.description.method.MethodDescription$AbstractBase.asTypeToken(MethodDescription.java:701)
at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default$Key$Harmonized.of(MethodGraph.java:881)
at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default$Key$Store.registerTopLevel(MethodGraph.java:1074)
at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.doAnalyze(MethodGraph.java:588)
at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.analyze(MethodGraph.java:548)
at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.analyzeNullable(MethodGraph.java:567)
at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.doAnalyze(MethodGraph.java:581)
at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.compile(MethodGraph.java:521)
at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$AbstractBase.compile(MethodGraph.java:442)
at net.bytebuddy.dynamic.scaffold.MethodRegistry$Default.prepare(MethodRegistry.java:480)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:160)
at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:153)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:2568)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$Delegator.make(DynamicType.java:2670)
at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:84)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$CachedBytecodeGenerator.getOrGenerateMockClass(TypeCachingBytecodeGenerator.java:91)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:38)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:67)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:38)
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:63)
at org.mockito.Mockito.mock(Mockito.java:1637)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:39)
at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:63)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:59)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:43)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:67)
... 23 more
Test ignored.
Process finished with exit code 255
答案 0 :(得分:4)
有同样的问题。当我将Java JDK版本更新为1.8.0_131时,错误消失了。
答案 1 :(得分:4)
这是因为Java / Mockito的兼容性。例如,如果您将Java 11与Mockito 2.17.0一起使用,它将抛出相同的错误(升级到Mockito 2.22.0或更高版本将解决此问题)。除了深入研究他们的release artifact on Github外,我还没有看到一份详尽的文件,其中阐明了兼容性矩阵。这是一个有用的兼容性列表(基于快速测试)
(脚注:*-不知道多远)
答案 2 :(得分:1)
我有同样的问题。 我尝试了堆栈溢出中列出的所有内容,即
这是https://github.com/mockito/mockito/issues/1606。
但是对我没有任何帮助。
我清除了所有缓存,然后重新安装了Android Studio 解决了这个问题。
可能只是清除Android Studio使用的所有缓存也可以解决。
答案 3 :(得分:1)
当我检查Java-8项目的依赖关系树时,我发现spring-boot-starter-test正在使用Mockito 2.15.0。通过排除该传递性Mockito依赖项并使用2.23.4版本显式重新添加(如果未自动完成,则进行重新构建)进行修复。我的Maven POM片段:
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
</dependency>
</dependencies>
如果使用gradle,它将类似于:
dependencies {
...
def withoutMockito = {
exclude group: 'org.mockito', module: 'mockito-core'
}
testImplementation "org.springframework.boot:spring-boot-starter-test:2.0.2-RELEASE", withoutMockito
testImplementation "org.mockito:mockito-core:2.23.4"
}
答案 4 :(得分:1)
对我来说,问题在于这些类必须为open
。
我正在使用:
@Mock lateinit var mock: ClassUnderTest
嘲笑的方法。
答案 5 :(得分:1)
对我来说,添加这个依赖解决了这个问题
androidTestImplementation "org.mockito:mockito-android:${versions.mockito}"
答案 6 :(得分:1)
确保在通过 Run As -> Run Configuration -> JRE 选项卡运行 junit 时正确选择了 jdk 版本。
答案 7 :(得分:0)
使用OpenJ9 JVM更新到新的Java版本后,我遇到了同样的问题,现在我使用的是HotSpot JVM。我没有这个问题了
答案 8 :(得分:0)
即使没有具体回答问题(问题是Java 8),我还是通过Google来到这里的。使用Java 11,我通过在build.gradle
中包含以下依赖关系解决了这个问题:
compile group: 'org.mockito', name: 'mockito-core', version: '2.22.0'
如here
所述答案 9 :(得分:0)
答案 10 :(得分:0)
更新JDK,将JDk 1.8更改为11时已修复此错误,我也添加了此
@Rule
public MockitoRule rule = MockitoJUnit.rule();
关于'告诉Mockito基于@Mock注释创建模拟'
答案 11 :(得分:0)
当我从JDK 13降级到1.8时,我遇到了同样的问题,错误消失了。我认为Mockito版本与版本13不兼容。
答案 12 :(得分:0)
我有同样的问题。
我的jdk版本是11.0.4+10-LTS
。
我使用org.springframework.boot:spring-boot-starter-parent:2.0.7.RELEASE
我将Mockito升级到3.3.0
异常仍然发生。
然后我这样做了:
从spring-boot-starter-test中排除的模仿核心
在pom文件下面添加
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
</exclusion>
<exclusion>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.10.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.10.5</version>
<scope>test</scope>
</dependency>
答案 13 :(得分:0)
我在 Android Studio 中遇到了同样的问题。
刚刚将 org.mockito:mockito-core
从 2.18.3
升级到 2.19.0
(截至目前最新)解决了我的问题。
答案 14 :(得分:-1)
在 android studio 中我遇到了这个错误,我清理了项目并重新启动了 IDE 来修复它。