Wednesday, January 25, 2006

Can't get 100% test coverage because of logging statements?

I was looking for a way that I can filter out all the logging statements such as:

if (log.isDebugEnabled())

and found that clover is capable of doing that, but as we're using Maven 2, the maven clover plugin doesn't seem to support it now, believe me, I've reviewed their source code.

Finally I found a solution that does the trick. In your test case, just set the logging level of the class under test to whatever you want, test your class and then set the logging level back! e.g. your class is foo.bar.MyClass


Logger logger = Logger.getLogger("foo.bar");
Level oldLevel = logger.getLevel();
logger.setLevel(Level.OFF);

// test your class here...

logger.setLevel(oldLevel);