简体   繁体   中英

Mocking static void method and changing its behaviour

I have a static void method from a different class that is invoked along my code as some sort of logger. In order to automatically test the different use cases of my code I was thinking of checking the logs of the system created by such method.

What I thought is to mock that class and overwrite the behaviour of the method so that it outputs to System.err . Also, redirecting System.err as explained here so that I can easily verify the correct functioning of the code.

I do not know how to override the standard behaviour of the method so that it does something (printing to the stderr) instead of what it normally does (by not mocking it) or nothing (what I get my using when(...).thenReturn(...) )

Mocking static method is not a good way to go, believe me.

Altough it is possible with for example PowerMock framework (it is going to replace class loader under the hood), but still it is discouraged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM