即使使用Scala Test使用@PrepareForTest,PowerMockito也会抛出ClassNotPreparedException

时间:2016-09-30 20:32:30

标签: scala powermock scalatest powermockito

我有以下测试...

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测试的特质吗?

1 个答案:

答案 0 :(得分:-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 {
}

等...