本文介绍了在initMocks上的Mockito AbstractMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我整天都在努力使Mockito可以用于我的Android项目.我将所有内容添加到了Gradle构建文件中:

So I've been struggling pretty much all day trying to get Mockito to work for my Android project.I added everything to my Gradle build file:

androidTestCompile 'org.mockito:mockito-core:2.0.29-beta'
androidTestCompile "junit:junit:4.12-beta-3"
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'

并尝试运行实际上没有任何作用的测试:

and have tried running a test that doesn't really do anything:

@RunWith(MockitoJUnitRunner.class)
public class LoginActivityTest extends 
    ActivityInstrumentationTestCase2<LoginActivity> {

    private LoginActivity loginActivity;

    private EditText et_email;
    private EditText et_password;
    private Button btn_login;

    @Mock
    SpiceManager manager;

    public LoginActivityTest(){
        super(LoginActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        loginActivity = getActivity();

        MockitoAnnotations.initMocks(this);
        //manager = mock(SpiceManager.class);
        loginActivity.spiceManager = manager;

        et_email = (EditText) loginActivity.findViewById(R.id.et_email);
        et_password = (EditText) loginActivity.findViewById(R.id.et_password);
        btn_login = (Button) loginActivity.findViewById(R.id.btn_login);
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    public void testLoginEmpty() throws Exception {        
        verify(manager).execute(
            any(LoginRequest.class), 
            anyString(), 
            anyLong(), 
            any(LoginActivity.LoginRequestListener.class));
    }
}

我要模拟该服务的原因是因为我想使网络部分不受测试影响.不需要实际发送网络请求以进行简单测试,对吧?

The reason I want to mock the service is because I would like to keep the network part out the test.There's no need to actually send a network request for a simple test, right?

无论如何,该应用会生成,但是当实际测试开始运行时,它会失败(甚至崩溃),并显示AbstractMethodError:

Anyhow, the app builds but when the actual test starts running it fails (or rather crashes) with an AbstractMethodError:

Running tests
Test running started
java.lang.AbstractMethodError: abstract method     
    "org.mockito.plugins.MockMaker$TypeMockability 
    org.mockito.plugins.MockMaker.isTypeMockable(java.lang.Class)"

at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:26)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:21)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:167)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:161)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:58)
at org.mockito.Mockito.mock(Mockito.java:1410)
at org.mockito.Mockito.mock(Mockito.java:1288)
at be.sanmax.membr.activities.LoginActivityTest.setUp(LoginActivityTest.java:50)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)

这使我感到奇怪,因为SpiceManager类不包含任何abstract方法.但是,它是我未编写的某些程序包的一部分(com.octo.android.robospice).但这不应该是一个问题.应该吗?

This strikes me as odd since the SpiceManager class does not contain any abstract methods.It is, however, part of some package that I didn't write (com.octo.android.robospice). But that shouldn't be an issue. Should it?

如果这是问题所在,我该如何从任何测试中排除它?我只想测试应用程序的工作,而不是网络连接...

And if that is the issue, how could I go about factoring it out from any tests? I only want to test the working of the app, not the network connection...

推荐答案

由于MockMaker的定义已更改,Dexmaker不支持Mockito 2.0.我建议您使用Mockito 1.10.19,但随后您将遇到此NPE 修复程序的.

Dexmaker does not support Mockito 2.0 since the definition of MockMaker has changed. I suggest you use Mockito 1.10.19 but then you will run into this NPE for which I have submitted a fix.

好像我的修复程序现在从1.5版合并到了dexmaker中

Looks like my fix is now merged into dexmaker as of version 1.5

这篇关于在initMocks上的Mockito AbstractMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:47