What does the annotation @PrepareForTest
in PowerMockito really mean?
What should be placed there apart of classes which have static methods?
Annotation Type PrepareForTest This annotation tells PowerMock to prepare certain classes for testing. Classes needed to be defined using this annotation are typically those that needs to be byte-code manipulated.
PowerMock is an open-source mocking library for Java applications. It extends the existing mocking frameworks, such as EasyMock and Mockito, to add even more powerful features to them. PowerMock enables us to write good unit tests for even the most untestable code.
While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.
That annotation tells PowerMock(ito) that the listed classes will need to be manipulated on the byte code level.
You need to "prepare for test" all these classes X of which you want to
new()
used in another class XPowerMockito.when(spy, "privateMethodNameAsString").then...
In other words:
X.doStatic()
, you have to prepare the class X.new Y(...)
, you have to prepare the class X that contains that new
statement.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With