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

查看:52
本文介绍了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

  • 模拟 static 方法(在 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天全站免登陆