2

Mocked the "test" method using powermockito, can I do the same with WhiteBox in powermockito?

Are there any APIs in WhiteBox to do similar thing?

public class MTest {

   @Before
  public void setUp() throws Exception {
    PowerMockito.whenNew(MCfg.class).withNoArguments().thenReturn(tCfg);
    doNothing().when(tCfg).test();
 }
}

-Thanks,

mrs
  • 207
  • 2
  • 5
  • 13
  • I put up a rather generic answer; as your input is simply missing the core part: the code you intend to test. Please put up a real [mcve]. Beyond that: some feedback on my other answers to some of your questions would be really appreciated. You can always let me know if there is something that I should do in order to achieve "upvote" worthiness for my answers. – GhostCat Apr 12 '17 at 07:00

1 Answers1

1

Your question isn't very clear about what you actually want to do; but I my advise: be really carefully about making you so dependent on your mocking frameworks.

The point is: that WhiteBox class is in a package named internal for a reason. Should you ever decide to move to PowerMockito v2 ... you will find that the WhiteBox class is gone! Where moving forward isn't that interesting; as PowerMockito doesn't work with any decently recent version of Mockito.

Thus, my serious advise: you are writing your own code. Then simply learn how to create testable code; for example by watching these videos.

I have made the experience that EasyMock or Mockito are completely sufficient to test code that is written to be testable. Using PowerMock without thinking about such aspects is most likely leading you to create less-than-optimal production code.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I have updated the question with a complete simple example as I needed to ask the similar question. Hope it represents the question effectively. – S Kumar Jul 02 '18 at 19:01
  • Just asked a new question as the edit was not approved. Link: https://stackoverflow.com/questions/51150117/how-to-mock-private-methods-using-whiteboxorg-powermock-reflect – S Kumar Jul 03 '18 at 08:43