0

I am getting the above exception when i am trying to mock the logger .

I want to verify the logs.

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class MedSlaveImpl implements MedSlaveInt {
    private static final Logger logger = LoggerFactory.getLogger(MedSlaveImpl.class);

@Override
    public void enterMode() {
        logger.info("muting the Manager");

    }

}

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import org.powermock.modules.junit4.PowerMockRunner;

import uk.org.lidalia.slf4jtest.TestLoggerFactory;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/context-test.xml", "/context.xml" })
@TestPropertySource("/config.properties")
@ActiveProfiles("mediator")
@PrepareForTest({ MedSlaveImpl.class, LoggerFactory.class })
public class MedModeTest {


    // For mediator
    @Test
    public void testStartService() throws Exception {


        mockStatic(LoggerFactory.class);
        Logger logger = mock(Logger.class);
        when(LoggerFactory.getLogger(any(Class.class))).thenReturn(logger);
        // TestLogger logger =
        // TestLoggerFactory.getTestLogger(MedSlaveImpl.class);

        mediatorImpl.enterMode();
        verify(logger).info("muting the Manager");
        // assertThat(logger.getLoggingEvents(),is(asList(info("muting theManager"))));
    }

    @After
    public void clearLoggers() {
        TestLoggerFactory.clear();
    }

}

I had spent whole day in to verify the logs in different approaches . like using

TestLogger logger, Mockitto , powermock . Finally i ended up with different exceptions.

And for the current approach i am getting the above exception .

Suggestions are greatly appreciated .Thanks for the help in advance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Avinash T S
  • 44
  • 1
  • 9
  • Rather than mocking you may want to check out this, https://gist.github.com/geowarin/3685379c1ae5f100a02c, to capture output logging and verify against it rather than trying to mock logging frameworks which is super painful. – Darren Forsythe Feb 19 '18 at 14:45
  • You can check the method described [here](https://stackoverflow.com/questions/8948916/mocking-logger-and-loggerfactory-with-powermock-and-mockito?answertab=active#tab-top) – RSingh Feb 19 '18 at 15:46
  • Thank you All for the quick help . I have tried the same thing and i am getting exception as illigal state exception. Can not subclass final class loggerfactory – Avinash T S Feb 19 '18 at 16:20
  • http://projects.lidalia.org.uk/slf4j-test/apidocs/index.html?uk/org/lidalia/slf4jtest/TestLoggerFactory.html – Avinash T S Feb 19 '18 at 18:35
  • http://projects.lidalia.org.uk/slf4j-test/apidocs/index.html?uk/org/lidalia/slf4jtest/TestLoggerFactory.html I have used the this approach also but testLogger has no effect and its giving null – Avinash T S Feb 19 '18 at 18:36

2 Answers2

0

You have a couple of options. One is to follow Darren's advice and capture the logged output and examine it. Another is to supply your own LoggerFactory that produces a logger to your liking. Note that SFL4J is an API, so that is doable. However, that seems like a lot of work for what you are trying to accomplish.

Steve11235
  • 2,849
  • 1
  • 17
  • 18
0

The solution without mocking of logger factory.

Have a method for lazy loading of the logger:

public class MedSlaveImpl implements MedSlaveInt {
    private static Logger logger;

    // Package visible to override it in the tests.
    Logger getLogger() {
        if (logger == null) {
            logger = LoggerFactory.getLogger(MedSlaveImpl.class);
        }
        return logger;
    }

    @Override
    public void enterMode() {
        // All calls to the logger via getLogger() method.
        getLogger().info("muting the Manager");
    }
}

Your test has an instance of your service class with overridden getLogger() method:

@Test
public void testStartService() throws Exception {
    //Setup
    final Logger mockedLogger = mock(Logger.class);

    final MedSlaveImpl service = new MedSlaveImpl() {
        @Override
        Logger getLogger() {
            return mockedLogger;
        }
    }

    // When
    service.enterMode();

    // Then
    verify(mockedLogger).info("muting the Manager");
}
Dmytro Maslenko
  • 2,247
  • 9
  • 16