AbstractMethod Error exception occurred in android Mockito upgrade 2.0

AbstractMethod Error exception occurred in android Mockito upgrade 2.0

Recently, in the research of android unit testing, I didn't expect the first step in the pit!!! When testing the model of mvp, using Mockito mock object, AbstractMethodError exception always occurs in initialization. Let's not mention the abnormal environment first.

androidTestImplementation "org.mockito:mockito-core:2.25.0"
androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'

Abnormal again:

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:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:240)
at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:228)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:61)
at org.mockito.Mockito.mock(Mockito.java:1907)
....

### Initialization mode 1:

@RunWith(MockitoJUnitRunner.class)
public class LoginFrgModelTest {

    @Mock
    List testList;
	
	 @Test
    public void doLoginByMsg() {
    
      Mockito.when(testList.get(0)).thenReturn("ssdf");
      String o = (String) testList.get(0);
      Assert.assertEquals(o, "ssdf");

	}
}

### Initialization mode 2:

public class LoginFrgModelTest {

   @Mock
   List testList;

   @Before
   public void setUp() throws Exception {
   
     MockitoAnnotations.initMocks(this);
   }

   @Test
   public void doLoginByMsg() {
    
     Mockito.when(testList.get(0)).thenReturn("ssdf");
     String o = (String) testList.get(0);
     Assert.assertEquals(o, "ssdf");

   }
}

### Initialization mode 3:

public class LoginFrgModelTest {
   @Test
   public void doLoginByMsg() {
     List testList = mock(List.class);
     Mockito.when(testList.get(0)).thenReturn("ssdf");
     String o = (String) testList.get(0);
     Assert.assertEquals(o, "ssdf");

   }
}

All kinds of postures all reported AbstractMethodError exceptions. They all doubted whether I was suitable for playing the test. Finally, I went to the University and found a trace of tricky. Dexmaker is no longer suitable for the Mockito 2 version!!!

Dexmaker does not support Mockito 2.0 since the definition of 
MockMaker has changed. 

bug to solve the problem
Decisively suspect that the following two libraries have problems

androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'

google continues to find that someone has written a compatible Mockito 2.0, decisively replacing. it worked.

androidTestImplementation "org.mockito:mockito-core:2.25.0"
//androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
//androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.25.0'

The end of
ps:
There is a saying on the Internet that Mode 2.0 is not good to use. No verification, because lazy.

Keywords: Java Google Android

Added by mikemessiah on Thu, 03 Oct 2019 02:35:29 +0300