android Mockito 升级2.0 发生 AbstractMethodError 异常
最近在研究android单元测试,没想到第一步进坑里了!!!在测试mvp之model的时候使用Mockito mock对象,在初始化一直发生AbstractMethodError异常.不说了先上异常环境.
androidTestImplementation "org.mockito:mockito-core:2.25.0"
androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
再上异常:
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)
....
###初始化方式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");
}
}
###初始化方式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");
}
}
###初始化方式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");
}
}
各种姿势玩遍,都报AbstractMethodError异常.都怀疑我是不是合适玩测试了.最后还是上大谷歌了,果然查到了一丝猫腻,Dexmaker已经不适合在Mockito 2.0版本了!!!
Dexmaker does not support Mockito 2.0 since the definition of
MockMaker has changed.
##解决bug
果断怀疑以下两个库有问题了
androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
继续google发现有人写了个兼容Mockito 2.0,果断替换.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'
#完
ps:
网上有说2.0方式二不好用了.没有验证,因为懒…