2

I am working on legacy code and writing some junit tests (I know, wrong order, never mind) using jmock (also wasn't my choice, nothing I can change about that) and I have class which does some elaborate logging, messing with Strings in general. We are using log4j for logging and I want to test those logged messages. I thought about mocking Logger class, but I don't know how to do it.

As usually we have Logger done like this:

private static final Logger LOG = Logger.getLogger(SomeClass.class);

Does anyone have any idea how to mock method .getLogger(class) or any other idea how to check what exactly has been logged?

NamshubWriter
  • 23,549
  • 2
  • 41
  • 59
Edheene
  • 455
  • 2
  • 7
  • 20

4 Answers4

4

You can write own appender and redirect all output to it.

Andrzej Jozwik
  • 14,331
  • 3
  • 59
  • 68
  • Ha! I have even done it this way some months back but forgot about it. This is what I'll do, thanks – Edheene Apr 20 '12 at 08:01
  • That works only if you use the standard logging. But if you use logging from some special platform, the operations with appenders could be impossible. And you have to mock. – Gangnus Jul 24 '22 at 10:00
3

If you really think you need to do this, then you need to take a look at PowerMock. More specifically, it's ability to mock static methods. PowerMock integrates with EasyMock and Mockito, but some hunting about might result in you finding a JMock integration too if you have to stick with that.

Having said that, I think that setting up your test framework so that it logs nicely without affecting your tests, and ensuring your tests do not depend upon what gets logged is a better approach. I once had to maintain some unit tests that checked what had been logged, and they were the most brittle and useless unit tests I have ever seen. I rewrote them as soon as I had the time available to do it.

Jon
  • 3,510
  • 6
  • 27
  • 32
  • 1
    +1 I too had to maintain tests that mocked the logger. Utter madness. – artbristol Apr 20 '12 at 07:19
  • Have to confess, about 80% of my "rewrite" was hitting the delete button ;-) – Jon Apr 20 '12 at 07:43
  • But this would mean changing tested class which I am not supposed to do (except of course situation if i find any bugs). I am also not happy about this situation, but there's nothing I can do about it right now, perhaps in future. thanks anyway – Edheene Apr 20 '12 at 08:01
  • Software is *soft* - you're supposed to be able to change it. Save yourself and your team some pain and go and talk to your team lead about making this change now, not later. What harm is there in asking? – Jon Apr 20 '12 at 08:09
  • And come to think of it - how would using PowerMock require you to change the tested class? – Jon Apr 20 '12 at 12:13
0

Check this similar question: How to mock with static methods?

And by the way, it is easier to search for an existing qusetion to your problem than to post a question and wait for answers.

Community
  • 1
  • 1
adranale
  • 2,835
  • 1
  • 21
  • 39
0

The easiest way I have found is using the mock log4j objects in JMockit.

You just need to the add the annotation

@UsingMocksAndStubs(Log4jMocks.class)

to your test class and all code touched by the tester class will be using a mock Logger object.

See this

But this wont log the messages. You can get away from the hassle of dealing with static objects with this.

n3o
  • 2,795
  • 5
  • 24
  • 37