我在测试中使用了ScalaTest和Mockito。如果我使用命令行,测试就通过了。但当我使用Intellij运行时,出现了以下错误。它似乎只为测试导入了mockito。
An exception or error caused a run to abort: org.mockito.mock.MockCreationSettings.getSerializableMode()Lorg/mockito/mock/SerializableMode;
java.lang.NoSuchMethodError: org.mockito.mock.MockCreationSettings.getSerializableMode()Lorg/mockito/mock/SerializableMode;
at org.powermock.api.mockito.repackaged.AcrossJVMSerializationFeature.enableSerializationAcrossJVM(AcrossJVMSerializationFeature.java:166)
at org.powermock.api.mockito.repackaged.CglibMockMaker.createMock(CglibMockMaker.java:23)
at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:46)
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 org.scalatest.mock.MockitoSugar$class.mock(MockitoSugar.scala:74)
我在Eclipse中遇到了完全相同的问题,这是因为我有org.mockito.mock.MockCreationSettings类的多个实例。这个类存在于mockito-core.jar和mockito-all.jar中。我不知道为什么开始失败,我在很长一段时间和很多测试中都使用了相同的配置,但问题是我不得不从pom.xml中删除mockito核心依赖项,只保留mockito all。希望能有所帮助
PS:您也可以运行mvn-dependency:tree来检查另一个依赖项是否也带来了mockito-core.jar
它正在MockCreationSettings
类中查找名为getSerializableMode()
的方法。你的Mockito和PowerMock版本之间似乎不匹配。
试着浏览PowerMock的Maven存储库,并检查您的PowerMock-api-Mockito所依赖的Mockito版本。
我使用的是IntelliJ ultimate 2019.3,scala 2.12.8。我也面临同样的问题。
升级图书馆对我来说很有效。之前:
"org.scalatest" %% "scalatest" % "3.0.5"
"org.mockito" %% "mockito-scala" % "1.5.12"
"org.mockito" %% "mockito-scala-scalatest" % "1.5.12"
之后:
"org.scalatest" %% "scalatest" % "3.1.1"
"org.mockito" %% "mockito-scala" % "1.13.0"
"org.mockito" %% "mockito-scala-scalatest" % "1.13.0"