What does @PrepareForTest in PowerMock really mean?

隐身守侯 提交于 2020-03-17 11:14:40

问题


What does the annotation @PrepareForTest in PowerMockito really mean?

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


回答1:


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 X. To control new Y, you have to prepare the class X that contains that new() statement.



来源:https://stackoverflow.com/questions/56430071/what-does-preparefortest-in-powermock-really-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!