I use slf4j extensively in my application. For stability reasons, there are scenarios where my code will gracefully handle an error condition and simply log it as an error and move on.
However, I want to write tests that demonstrate that, for given input, my SUT does not generate any such error logs.
For example, my code often looks like this:
public class MyClass {
private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);
// snip
public void someMethod(DTO input) {
try {
// snip
} catch (SomeException e) {
LOG.error("Code failed here!", e);
// recover gracefully
}
}
}
I want to, in my test case, catch something like
Mockito.verify(LOG.error(Mockito.anyString(), Mockito.any(SomeException.class), times(0));
However, because of the way I generate the LOG
object - using the slf4j LoggerFactory
api, it's difficult to mock the LOG
object. What I'm really looking to do is have a different testing slf4j implementation (I use logback in production) that JUnit and Mockito have access to. How can I do this?
I am using JUnit 4.11, Mockito 1.9.5, and Jukito 1.3. I'd prefer not to use PowerMock as it's incompatible with Jukito.