Capturing Log information in Mockito

  Kiến thức lập trình

I’m writing unit tests for a Java 8 project with testng and Mockito. I have a class that I’m testing that initializes the Logger without a constructor as such:

class MyTestClass {
    protected static final Logger LOG = LogManager.getLogger("MYLOGGER");
    
    public void someMethod(){
    LOG.info("This should be logged");    
    }
}

I know if the Logger is passed as an argument to the constructor, I can use a spy. But this is legacy code, and I can’t add a constructor, unfortunately.
I’m wondering if it’s possible to capture and verify the content of the log message in a test?
I would greatly appreciate any help with this. Thank you.

LEAVE A COMMENT