PowerMock in scala

I want to mock scala companion object, so I am trying to use PowerMockito. I have the following:

import org.junit.runner.RunWith
import org.mockito.Mockito.when
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.powermock.api.mockito.PowerMockito._

@RunWith(classOf[PowerMockRunner])
@PrepareForTest(Array(classOf[ClassToMock]))
class Test {

describe("test") {
  it("test") {
    mockStatic(classOf[ClassToMock])
    when(ClassToMock.apply()).thenReturn(null)
  }
}

      

I get the following error when running the test:

The class path.to.ClassToMock 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: 

      

Any ideas on how I can manage to make the annotation?

thank!!

+3


source to share





All Articles