【问题标题】:PowerMockito throwing ClassNotPreparedException even with @PrepareForTest using Scala Test即使使用 Scala 测试的 @PrepareForTest,PowerMockito 也会抛出 ClassNotPreparedException
【发布时间】:2017-02-09 12:17:54
【问题描述】:

我有以下测试...

import org.scalatest.junit.JUnitRunner
...
@PowerMockRunnerDelegate(classOf[JUnitRunner])
@PrepareForTest(Array(classOf[AuditLog]))
class ConnectorAPITest extends path.FreeSpec with ShouldMatchers {
  "Mocked Tests" - {
      println("This got called in the mocked tests.")
      PowerMockito.mockStatic(classOf[AuditLog]);
      ...
  }
}

但是当我跑步时,我得到...

An exception or error caused a run to abort: The class com.paxata.services.log.AuditLog not prepared for test.
To prepare this class, add class to the '@PrepareForTest' annotation.
In case if you don't use this annotation, add the annotation on class or  method level. 
org.powermock.api.mockito.ClassNotPreparedException: 
The class com.paxata.services.log.AuditLog not prepared for test.
To prepare this class, add class to the '@PrepareForTest' annotation.

鉴于注释已经存在,这没有意义?它是 Scala 测试的特质吗?

【问题讨论】:

  • 如果你尝试在注解中使用完整的类路径会发生什么?
  • 你可以试试@PrepareForTest({AuditLog.class}) 代替那个。

标签: scala powermock scalatest powermockito


【解决方案1】:

我在使用 FunSuite 时遇到了同样的问题。当我转向 JUnit 时,它就可以工作了。

@RunWith(classOf[PowerMockRunner])
@PrepareForTest(Array(classOf[SomeStaticClass]))
class MyTestClass {

  @Before
  def setUp {
    PowerMockito.mockStatic(classOf[SomeStaticClass])
    Mockito.when(SomeStaticClass.getSomeObject(1)).thenReturn(new SomeObject(1))
  }

@Test
def someTestMethod {
}

等等……

【讨论】:

  • 但是很高兴知道如何使它与 ScalaTest 一起工作
  • 我尝试了同样的事情,但 scala test 找不到测试。
  • 啊,我明白了。这对我来说不是问题,我正在 IntelliJ Idea 中使用 junit runner 运行测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2019-01-17
  • 1970-01-01
  • 2016-08-13
  • 2020-04-15
  • 1970-01-01
相关资源
最近更新 更多