NoClassDefFoundError for MockitoInvocationHandler class

梦想的初衷 提交于 2019-12-21 12:18:50

问题


I am using mockito-all-1.9.5-rc1.jar and powermock-mockito-1.4.12-full.jar. When I run this simple unit test for mocking final method in non-final class.

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ABC.class)
public class ABCTest {

    @Test
    public void finalCouldBeMock() {
        final ABC abc = PowerMockito.mock(ABC.class);
        PowerMockito.when(abc.myMethod()).thenReturn("toto");
        assertEquals("toto", abc.myMethod());
    }

}

When I ran it, I got java.lang.NoClassDefFoundError: org/mockito/internal/MockitoInvocationHandler Caused by: java.lang.ClassNotFoundException: org.mockito.internal.MockitoInvocationHandler

When I search fo class MockitoInvocationHandler in mockito-all-1.9.5-rc1.jar and powermock-mockito-1.4.12-full.jar. I couldn't find any. Need help with this issue! Thank you


回答1:


Mockito 1.9.5-rc1 had to be refactored internally to allow third party mock maker. MockitoInvocationHandler was part of the Mockito's internals (as the package name suggests) up to Mockito 1.9.0.

Due to these changes, current some older version Powermock releases as of today are not compatible with the latest Mockito release.

Another reason to avoid mocking/stubbing finals or statics ;)

Hope that helps Cheers,



来源:https://stackoverflow.com/questions/12157559/noclassdeffounderror-for-mockitoinvocationhandler-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!