Mockito/PowerMock 兼容性问题

Mockito / PowerMock compability issue

我收到以下错误:

java.lang.NoSuchMethodError: org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework

自从我将 Mockito 版本从 1.10.19 切换到 3.1.0 并将 PowerMock 版本从 1.7.4 切换到 2.0.4 后,尝试 运行 JUnit 测试时。

我也试过使用 Mockito 2.2.28 版和 PowerMock 2.0.4 版,得到了同样的错误。

在每个 class 我都有以下注释,也许这可以帮助你:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Some.class)

我在@second 的帮助下发现了错误:

You should try it to figure out what your real problem is. If it still does not work it probably has something to do with your depency management system. If it does work you can try newer versions and see whether some tests that worked before are now broken

错误:

Yup, you were right. Older version of mockito was inherited from a parent pom file, but even though i excluded mockito-core from that jar file, powermock still used that old version of mockito-core and not the new one. So i also excluded mockito-core from powermock too and now it works