How to setup mockito to capture logging events when using @slf4j?

3,158 views
Skip to first unread message

Sharon Snyder

unread,
Sep 18, 2019, 6:27:59 PM9/18/19
to Project Lombok
Hi All,

I've started using Lombok fairly recently compared to many of you....and I am in the process of updating some code that is a few years old. As my very first step, I have added Lombok to set up the slf4j logger (and I am using a lombok.config file to rename the variable to LOGGER vs. log, so my existing code still works).  When I run my tests, it fails when stating that LOGGER cannot be resolved.  Here is a snippet of how I set up my capture:


    @Mock private Appender mockAppender;
    @Captor private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        this.carp = spy(new MyCodeProcessor(mock(MyClass.class), mock(AnotherClass.class), mock(AndOneMoreClass.class)));
        //setup the logger so I can verify what is output to the logfile
        Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        root.addAppender(mockAppender);
        root.setLevel(Level.INFO);
    }


I know that this code did work previously, so I am fairly certain it is related to adding Lombok to the mix.  Has anyone tried to do log capturing as a part of their tests with Lombok.  Before anyone complains that this is not proper testing, the code should be refactored, etc. This is not my priority at this time - I just wanted to start the process with what I thought was simple - adding Lombok for the logging before moving on to the rest of the refactoring that has to get done.

Thanks in advance,
Sharon

Reinier Zwitserloot

unread,
Sep 19, 2019, 4:53:45 AM9/19/19
to Project Lombok
I didn't think mockito works on source files, only on class files, in which case lombok's existence should have no effect (the produced class files are identical, or should be). If so, can you doublecheck, for example with the javap tool, that you that LOGGER field? Lombok also makes them static, and private. I confess my experience with mockito is very limited :(

Sharon Snyder

unread,
Sep 19, 2019, 9:11:22 AM9/19/19
to Project Lombok
Thanks, I'll look into this.  I have a feeling it may be related to only migrating some of the files to using Lombok and leaving others alone that I had not yet needed to change.

Sharon
Reply all
Reply to author
Forward
0 new messages