Spy object by Mockito in Spring

后端 未结 4 478
眼角桃花
眼角桃花 2020-12-20 17:56

When I try to Spy an object in my unit test, I got an exception. This is my unit test file:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loc         


        
相关标签:
4条回答
  • 2020-12-20 18:38

    It might be problem with the dependencies. The powermock-api-mockito has compile time dependency on mockito-core version 1.10.19, but you have also defined a dependency on version 2.0.44-beta. This might be a classpath problem. Try removing the explicit dependency on version 2.0.44-beta.

    0 讨论(0)
  • 2020-12-20 18:38

    This is happening because of the dependency of powermock-api-mockito on mockito-core . Since powermock-api-mockito is added explicitly as dependency please remove mockito-core, this might be causing the issue

    0 讨论(0)
  • 2020-12-20 18:41

    Use powermock-api-mockito2 if you are using 2.x.x version of mockito-core as powermock-api-mockito is not compatible with mockito-core 2.

    0 讨论(0)
  • 2020-12-20 18:53

    In the release 2.0.32-beta Mockito team moved cglib classes into separated jar. But PowerMock still depends on classes which have been moved, so you get java.lang.NoClassDefFoundError.

    In release 1.6.2 Mockito classes was copied into PowerMock, but not all. As workaround I may suggest clone, build and add to classpath the mockito-cglib (https://github.com/mockito/mockito-cglib).

    The PowerMock will have supported Mockito 2 API since 1.6.5 which will have been released by the end of the May, but will still use cglib. The full Mockito 2 with Bytebuddy will be supported in release after 1.6.5.

    Developer Guide for Migration to Mockito 2

    0 讨论(0)
提交回复
热议问题