1

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(...))

Community
  • 1
  • 1
user2891462
  • 3,033
  • 2
  • 32
  • 60
  • Don't test log output unless that's what you really want to test. Why not test the *behavior* of the method? – Ingo Bürk Jul 03 '14 at 17:38
  • The behaviour of the method is very hard to test: it connects to a server, sends a message and displays it on the GUI, but I do not want to test the GUI. – user2891462 Jul 03 '14 at 19:34
  • Then test only the non-GUI behavior. If necessary, refactor your code to make it testable. Sounds like your method is doing way too much anyway. – Ingo Bürk Jul 03 '14 at 19:40

1 Answers1

4

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.

G. Demecki
  • 10,145
  • 3
  • 58
  • 58
  • 2
    Amen. We used PowerMock and it just got us into a lot of trouble -- and even more trouble when we decided to remove it and had to change hundreds of tests. – Ingo Bürk Jul 03 '14 at 19:41