模拟:无法模拟界面



我有一个公共接口Repository,当我尝试使用 mockito @Mock它时,它给出了以下错误

            org.mockito.exceptions.base.MockitoException: 
            Mockito cannot mock this class: interface com.my.package.repository.Repository.
            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    : JetBrains s.r.o
            JVM vendor version : 25.152-b01
            JVM name           : OpenJDK 64-Bit Server VM
            JVM version        : 1.8.0_152-release-915-b01
            JVM info           : mixed mode
            OS name            : Linux
            OS version         : 4.4.0-109-generic

            Underlying exception : java.lang.IllegalArgumentException: Could not create type

以下是完整的堆栈跟踪日志

    Underlying exception : java.lang.IllegalArgumentException: Could not create type
    at org.mockito.internal.runners.DefaultInternalRunner$1.withBefores(DefaultInternalRunner.java:38)
    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.DefaultInternalRunner$1.run(DefaultInternalRunner.java:68)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:74)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  Caused by: java.lang.IllegalArgumentException: Could not create type
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:140)
    at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)
    at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)
    at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:32)
    at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:71)
    at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:42)
    at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
    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:1729)
    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.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:38)
    at org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:62)
    at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:57)
    at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)
    at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:69)
    ... 19 more
  Caused by: java.lang.NoClassDefFoundError: android/net/Uri
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at net.bytebuddy.description.method.MethodList$ForLoadedMethods.<init>(MethodList.java:90)
    at net.bytebuddy.description.type.TypeDescription$ForLoadedType.getDeclaredMethods(TypeDescription.java:7232)
    at net.bytebuddy.description.type.TypeDescription$Generic$OfNonGenericType.getDeclaredMethods(TypeDescription.java:3349)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.doAnalyze(MethodGraph.java:576)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.analyze(MethodGraph.java:537)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.doAnalyze(MethodGraph.java:573)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$Default.compile(MethodGraph.java:508)
    at net.bytebuddy.dynamic.scaffold.MethodGraph$Compiler$AbstractBase.compile(MethodGraph.java:408)
    at net.bytebuddy.dynamic.scaffold.MethodRegistry$Default.prepare(MethodRegistry.java:418)
    at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:162)
    at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:155)
    at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:2560)
    at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$Delegator.make(DynamicType.java:2662)
    at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:94)
    at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:37)
    at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:34)
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
    ... 36 more
  Caused by: java.lang.ClassNotFoundException: android.net.Uri
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 56 more

正如错误消息中所说,Repository是一个非私有和非最终接口。但是为什么它会给出这个错误?

我的接口方法返回RxJava可观察量。这对莫比托有什么问题吗?

下面是示例测试代码

public interface Repository {
     Maybe<Integer> getUserCount();
 }

测试

@RunWith(MockitoJUnitRunner.class)
 public class LoginPresenterTest {
   @Mock
   Repository mRepository;
  @Test
  public void repoTest() {
    when(mRepository.getUserCount()).thenReturn(Maybe.just(1));
 }
}

这些是莫米托和朱尼特版本

testCompile "junit:junit:4.12"
testCompile "org.mockito:mockito-core:2.7.20"

我正在使用Java 8

以下是我的项目依赖项

    testCompile "junit:junit:$rootProject.ext.junitVersion"
testCompile "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"
implementation 'org.apache.commons:commons-lang3:3.5'
implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:design:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:cardview-v7:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:multidex:$rootProject.ext.multidexVersion"
implementation "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
implementation "com.google.dagger:dagger-android-support:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.ext.daggerVersion"
implementation "com.squareup.picasso:picasso:$rootProject.ext.picassoVersion"
implementation "com.squareup.retrofit2:retrofit:$rootProject.ext.retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$rootProject.ext.retrofitVersion"
implementation "com.squareup.okhttp3:okhttp:$rootProject.ext.okHttpVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.ext.okHttpVersion"
implementation "com.jakewharton.picasso:picasso2-okhttp3-downloader:$rootProject.ext.okhttp3DownloaderVersion"
debugCompile "com.squareup.leakcanary:leakcanary-android:$rootProject.ext.leakCanaryVersion"
releaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:$rootProject.ext.leakCanaryVersion"
testCompile "com.squareup.leakcanary:leakcanary-android-no-op:$rootProject.ext.leakCanaryVersion"
implementation "com.jakewharton:butterknife:$rootProject.ext.butterKnifeVersion"
annotationProcessor "com.jakewharton:butterknife-compiler:$rootProject.ext.butterKnifeVersion"

implementation "com.google.firebase:firebase-messaging:$rootProject.ext.firebaseVersion"
implementation group: 'joda-time', name: 'joda-time', version: '2.9.7'
implementation "io.reactivex.rxjava2:rxjava:$rootProject.ext.rxJavaVersion"
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.ext.rxAndroidVersion"
implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.ext.retrofitRxJavaAdapterVersion"


    minSdkVersion = 19
targetSdkVersion = 27
compileSdkVersion = 27
buildToolsVersion = '27.0.2'
supportLibraryVersion = '27.0.2'
rxJavaVersion = '2.1.6'
rxAndroidVersion = '2.0.1'
junitVersion = '4.12'
mockitoVersion = '2.7.20'
picassoVersion = '2.5.2'
daggerVersion = '2.12'
retrofitVersion = '2.2.0'
retrofitRxJavaAdapterVersion = '2.3.0'
okHttpVersion = '3.6.0'
okhttp3DownloaderVersion = '1.1.0'
leakCanaryVersion = '1.5.4'
butterKnifeVersion = '8.8.1'
multidexVersion = '1.0.1'
firebaseVersion = '10.2.6'

我遇到了类似的问题,事实证明我缺少这个包:https://central.sonatype.com/artifact/org.mockito/mockito-android/5.3.1

对于 Kotlin DSL 应用程序 gradle androidTestImplementation("org.mockito:mockito-android:5.3.1")

单独的问题,但相关:

然后对我来说,文档中的{on} {doReturn}不起作用。

并且when需要像 `when` 这样的反勾号进行转义,因此它不使用 Kotlin 语言的内置when

相关内容

  • 没有找到相关文章

最新更新