lundi 3 août 2015

How can I mock a private static method with PowerMockito?

This is the same question as found here:

How can I mock private static method with PowerMockito?

But the accepted answer isn't working for me. I have a static utility class with private methods that I need to test. I find that when I mock methods like this:

PowerMockito.spy(StaticUtil.class);
PowerMockito.when(StaticUtil.class, "getSomethingMethod", someObjectArray, someStringArray, aBoolean, someList).thenReturn(anotherList);

I'm getting a null pointer exception because the getSomethingMethod() is actually being called. When I debug, I see that it isn't being called when I run the method I'm trying to test, but it is actually running when I am setting up the mock. Based on this site (http://ift.tt/1BuVrRp) it looks like that is what is supposed to happen when you create the mock in this format.

So then I try to set up the mock this way:

PowerMockito.spy(StaticUtil.class);        
PowerMockito.doReturn(anotherList).when(StaticUtil.getSomethingMethod( someObjectArray, someStringArray, aBoolean, someList);

But I am getting an error from Eclipse that says I need to change the visibility of getSomethingMethod() to public. Isn't one of the big benfeits of using PowerMockito that you can mock private methods? I need to mock this private static method (without actually calling the method during setup). Any ideas?

Aucun commentaire:

Enregistrer un commentaire