转载

使用eclipse配置mockito的麻烦.给出错误:java.lang.verifyError

当我将我的mockito库添加到类路径,并使用一个简单的mockito示例进行测试时,我尝试使用模拟对象为函数add返回错误的值,我得到了 java

.lang.verifyerror.以下是用于测试后跟logcat的代码.

@Test
    public void testadd()
    {
        maincode obj2=mock(maincode.class);
        when(obj2.add(0, 0)).thenReturn(9);
        assertEquals(obj2.add(0, 0),9); 
    }

我收到以下错误.请帮忙!谢谢.

java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils  at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)  at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)  at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)  at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)  at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)  at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)  at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)  at org.mockito.cglib.proxy.Enhancer.(Enhancer.java:70)  at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:85)  at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)  at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56)  at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)  at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)  at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51)  at org.mockito.Mockito.mock(Mockito.java:1243)  at org.mockito.Mockito.mock(Mockito.java:1120)  at testaddmock.testadd(testaddmock.java:24)  at java.lang.reflect.Method.invokeNative(Native Method)  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
默认情况下,Mockito使用cglib创建动态代理,但对于Android,这不起作用,因为cglib生成.class文件,而不是.dex.但是从版本1.9.5开始,Mockito提供了一个允许替换代理生成引擎的扩展点,因此您只需要更改此引擎,我认为 Dexmaker 是最好的变体.

因此,为了使其有效,您应该只将 dexmaker-mockito-1.0.jar 添加到您的项目中,Mockito将使用它来生成他的代理.

翻译自:https://stackoverflow.com/questions/20311976/trouble-configuration-of-mockito-with-eclipse-gives-error-java-lang-verifyerro

原文  https://codeday.me/bug/20190112/519657.html
正文到此结束
Loading...