What does @PrepareForTest in PowerMock really mean?

后端 未结 1 1136
春和景丽
春和景丽 2020-12-14 17:37

What does the annotation @PrepareForTest in PowerMockito really mean?

What should be placed there apart of classes which have static methods?

相关标签:
1条回答
  • 2020-12-14 18:07

    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

    • mock a static method (on X)
    • gain control over calls to new() used in another class X
    • gain control over private methods (which you do using a spy and
      PowerMockito.when(spy, "privateMethodNameAsString").then...

    In other words:

    • To mock X.doStatic(), you have to prepare the class X.
    • To control new Y(...), you have to prepare the class X that contains that new statement.
    0 讨论(0)
提交回复
热议问题