PowerMock中的@PrepareForTest到底是什么意思? [英] What does @PrepareForTest in PowerMock really mean?

查看:3315
本文介绍了PowerMock中的@PrepareForTest到底是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerMockito中的注释@PrepareForTest到底是什么意思?

What does the annotation @PrepareForTest in PowerMockito really mean?

在具有静态方法的类之外,应该放在哪里?

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

推荐答案

该注释告诉PowerMock(ito)列出的类将需要在字节码级别上进行操作.

That annotation tells PowerMock(ito) that the listed classes will need to be manipulated on the byte code level.

您需要准备测试"所有您要测试的X类

You need to "prepare for test" all these classes X of which you want to

  • 模拟一个静态方法(在X上)
  • 控制对使用的 new() 的调用在另一个班级X
  • 控制私有方法(您可以使用间谍
    PowerMockito.when(spy, "privateMethodNameAsString").then...
  • 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...

换句话说:

  • 要模拟X.doStatic(),您必须准备X类.
  • 要控制new Y(...),您必须准备包含 new语句的类X.
  • 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.

这篇关于PowerMock中的@PrepareForTest到底是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆