Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

java - What does @PrepareForTest in PowerMock really mean?

What does the annotation @PrepareForTest in PowerMockito really mean?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...