J'essaie de me moquer d'une classe publique, mais ce faisant, Mockito lance un Mockito cannot mock this class
exception.
Classe que je souhaite moquer: https://github.com/scribejava/scribejava/blob/master/scribejava-core/src/main/Java/com/github/scribejava/core/builder/ServiceBuilder.Java
Code de test:
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)));
}
}
Trace de pile complète:
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
Eu le même problème. Lorsque j'ai mis à jour ma Java version JDK vers 1.8.0_131, l'erreur a disparu.
J'ai eu le même problème. J'ai essayé tout ce qui est répertorié sur le débordement de la pile, c'est-à-dire
et ceci: https://github.com/mockito/mockito/issues/1606 .
Mais rien n'a fonctionné pour moi.
J'ai effacé tout le cache, puis j'ai réinstallé Android studio qui a résolu le problème.
Il suffit probablement d'effacer tous les caches utilisés par Android Studio).
J'ai eu le même problème après avoir mis à jour vers la nouvelle version Java avec OpenJ9 JVM, maintenant j'utilise HotSpot JVM. Je n'ai plus ce problème