Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the common exceptions when using PowerMock with Mockito 2.x


What are the common exceptions when using PowerMock with Mockito 2.x


When using PowerMock with Mockito 2.x, several common exceptions and issues can arise due to compatibility problems and changes in the Mockito API. Here are some of the most common exceptions and how to address them:

1. NoClassDefFoundError: org.mockito.cglib.proxy.MethodInterceptor**

This exception occurs when using the wrong PowerMock API extension. The `powermock-api-mockito` extension does not work with Mockito 2.x, leading to this error. To fix it, you should use the `powermock-api-mockito2` extension instead[1][2].

2. Incompatible Versions of Mockito and PowerMock**

Using incompatible versions of Mockito and PowerMock can lead to various exceptions. For example, PowerMock 1.6.5 has experimental support for Mockito 2.x, but it's not reliable. Ensure you use compatible versions, such as PowerMock 1.7.0 RC2 with Mockito 2.1.0[1][2].

3. IllegalAccessError with Mockito 2.7.1**

When using PowerMock 1.6.5 or 1.7.0 RC2 with Mockito 2.7.1, you might encounter an `IllegalAccessError`. This issue is resolved by using PowerMock 1.7.0 RC4[1][4].

4. RuntimeExceptionProxy in Mockito 2.x with PowerMock**

In Mockito 2.x with PowerMock, original exceptions are wrapped as `RuntimeExceptionProxy`. This requires modifying test annotations from `@Test(expected=SomeException.class)` to `@Test(expected=Exception.class)` to catch the wrapped exceptions[1].

5. FieldNotFoundException with PowerMock’s Whitebox**

Mockito 2.x removed Whitebox, so using PowerMock’s Whitebox can lead to `FieldNotFoundException`. If this occurs, consider writing your own implementation or refactoring your code for better testability[1][2].

6. UnfinishedStubbingException**

This exception can occur even when stubbing seems correct. Ensure that there are no inline mock creations within stubbing methods and that all stubbing is properly completed with methods like `thenReturn()` or `thenThrow()`[3].

7. Compatibility Issues with Newer Mockito Versions**

PowerMock may not be compatible with newer Mockito versions, such as Mockito 4.0.0. In such cases, using an older version of Mockito that is compatible with PowerMock might be necessary[10].

Overall, migrating to Mockito 2.x from Mockito 1.x with PowerMock involves addressing these compatibility issues and potentially refactoring tests to avoid using PowerMock altogether[1][4].

Citations:
[1] https://proandroiddev.com/mockito-2-x-over-powermock-migration-tips-and-tricks-top-ten-118c52abd1d8
[2] https://dzone.com/articles/mockito-2x-over-powermock-migration-top-10-tips-and-tricks
[3] https://stackoverflow.com/questions/33441494/mockito-powermockito-weird-stubbing-exception
[4] https://proandroiddev.com/mockito-2-x-migration-session-recording-droidcon-uk-2018-ba70619f3811
[5] https://github.com/powermock/powermock/issues/706
[6] https://github.com/mockito/mockito/issues/1606
[7] https://stackoverflow.com/questions/69394651/cannot-run-powermock-due-to-unexpected-exception-and-org-mockito-exceptions-m
[8] https://stackoverflow.com/questions/61208743/powermock-with-mockito-dependencies-issue
[9] https://groups.google.com/g/powermock/c/3g5z_T1RDZQ
[10] https://github.com/powermock/powermock/issues/1109