【问题标题】:How can I get Mockito working in androidTest如何让 Mockito 在 androidTest 中工作
【发布时间】:2015-11-04 00:56:26
【问题描述】:

在我的测试中如何模拟我写的类?

这是我的测试代码:

import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

@RunWith(AndroidJUnit4.class)
public class SampleTest {
    @Mock
    Sample mySample;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void shouldMockSample() {
        verify(mySample, never()).neverCallMe();
    }

}

Sample 只是一个虚拟类。

public class Sample {

    public void neverCallMe() {}
}

如何摆脱这个错误?

java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils 在 org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167) 在 org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) 在 org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) 在 org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145) 在 org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117) 在 org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109) 在 org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105) 在 org.mockito.cglib.proxy.Enhancer.(Enhancer.java:70) 在 org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95) 在 org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57) 在 org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49) 在 org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24) 在 org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33) 在 org.mockito.internal.MockitoCore.mock(MockitoCore.java:59) 在 org.mockito.Mockito.spy(Mockito.java:1368)

在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runners.Suite.runChild(Suite.java:128) 在 org.junit.runners.Suite.runChild(Suite.java:27) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:115) 在 android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) 在 android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)

java.lang.NoClassDefFoundError: org/mockito/internal/creation/cglib/ClassImposterizer$3 at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95) 在 org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57) 在 org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49) 在 org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24) 在 org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33) 在 org.mockito.internal.MockitoCore.mock(MockitoCore.java:59) 在 org.mockito.Mockito.spy(Mockito.java:1368)

在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runners.Suite.runChild(Suite.java:128) 在 org.junit.runners.Suite.runChild(Suite.java:27) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:115) 在 android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) 在 android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)

我在我的 gradle 文件中使用以下依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
    androidTestCompile 'com.android.support:support-annotations:22.2.1'
    androidTestCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'org.objenesis:objenesis:2.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.0'
}

【问题讨论】:

    标签: android unit-testing mockito


    【解决方案1】:

    默认情况下,Mockito 使用 CGLib 或 ByteBuddy,两者都生成 .class 文件。您在 Android 设备或模拟器上运行,所以 .class 文件无济于事;你需要.dex 格式。

    调整您的依赖项以使用 DexMaker,这将覆盖 Mockito 的默认设置并允许在 Android 环境中进行模拟。

    【讨论】:

    • 非常感谢!添加 dexmaker 让我通过了错误并通过了示例测试。
    • 你见过@Mock 不起作用但 mock(object) 起作用的情况吗?
    • @ericharlow 我有,但不是你明确的@Before 调用MockitoAnnotations.initMocks(this)。它仍然看起来像这样,还是您重构为使用 MockitoRule 或 MockitoJUnitRunner? (您可能想作为一个单独的问题提出。)
    • 当我尝试在新的测试支持库中初始化模拟时出错。得到这个异常 java.lang.ExceptionInInitializerError。不知道为什么
    【解决方案2】:

    从 2.6.+ 版本开始,Mockito 添加了一个适用于 Android 的新工件,无需任何其他依赖项(即不再需要导入 DexMaker)。 (Reference)

    只需使用 org.mockito:mockito-android 作为您的插桩单元测试的依赖项 (androidTest)。您仍然可以使用通常的 org.mockito:mockito-core 进行本地单元测试。

    例子:

    dependencies {
        ...
        testCompile 'org.mockito:mockito-core:2.7.15'
        androidTestCompile 'org.mockito:mockito-android:2.7.15'
        ...
    }
    

    【讨论】:

    • 我不这样做。仍然遇到问题
    • 使用androidTestImplementation 而不是androidTestCompile,因为它现在已被弃用。
    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 2013-01-04
    • 2014-05-14
    • 1970-01-01
    相关资源
    最近更新 更多