hveiga
hveiga

Reputation: 6925

Mocking private static method from a final class (utility class)

I am using Mockito and PowerMock to do some testing to my Java app. I was wondering how I can mock a private static method from a final class. I want to simulate an Exception when accessing one method but the only way I have found to do it is if the class can be instantiated and using the spy() method from PowerMock.

Could anyone help/guide me a little?

Upvotes: 2

Views: 587

Answers (1)

Peter Kofler
Peter Kofler

Reputation: 9458

Here is an example using PowerMock to mock public static methods.

In case of a private method there must be some public static method somewhere to mock that calls this private method, as you can not call it directly. If you are only interested in an exception, you might throw the exception earlier (maybe - many assumptions)

Upvotes: 1

Related Questions