9

I want to mock a scala companion object, so im 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)
  }
}

When running the test i get the following error:

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 can i manage to make the annotation work?

Thx!!

9uzman7
  • 409
  • 8
  • 19
  • In order to see if that is actually possible, I would start on the java side; just to see if you can write a working JUnit test that uses the class name (maybe even using reflection). Beyond that, there is also JMockit ... the other framework able to mock static stuff. That one wouldnt push outdated versions of Mockito down your throat ;-) at least. – GhostCat Apr 11 '17 at 17:49
  • I mananged to test with PowerMockito in java for a static method. But in scala im not figuring out the problem with scala objects. – 9uzman7 Apr 11 '17 at 18:02
  • @9uzman7 did you any solution for this.? – TheCodeCache Dec 25 '18 at 18:02
  • 1
    @Manoranjan no, i changed my implementation in order to be able to unit test. – 9uzman7 Dec 25 '18 at 18:41

0 Answers0