Using Mockito to simulate a recorder in Java

Yes, you read correctly: Simulating the recorder in Java. "When you first see this message, it may sound strange, but in some special cases, unit tests are performed on certain parts of the application The only way is by checking whether certain messages are recorded.

For example, suppose your application needs to make asynchronous calls to some other WebServices. You will then have a separate thread for making this call. For the main thread, it does not matter whether the call to the service succeeds, because it will not wait for a response (otherwise it will be synchronous, right?). Suppose that this part of your application will only call the service, and then only record whether the call was successful (it goes without saying that in this case, you need a monitoring tool to know when the situation will not happen).

Now, you must create a JUnit test case to cover these scenarios. If you use Google search, you will find some use dynamic simulation for this, but please imagine you are already using Mochito and for some reason, you want to use Mochito as long as you want.

In this article, I assume that you are already familiar with Mochito, so I will focus on the "Analog Recorder" section. You can find many Mochito if you want to learn tutorials on the Internet.

Ok. One way to achieve this is to simulate the appender your logger object and use Mochito's ArgumentCaptor to capture all log events in the test case:

    @Mock
    private Appender mockedAppender;

    @Captor
    private ArgumentCaptor<LoggingEvent> loggingEventCaptor;

Then, you must ensure that the appender is added to the root recorder before performing the test:

    @Before
    public void setup() {
        Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        root.addAppender(mockedAppender);
        root.setLevel(Level.INFO);
    }

After that, you can call the test class and assert the recorded message:

    @Test
    public void testSuccessCall() throws Exception {
        // mock all required response objects in here

        [...]

        // this is the call to the service being tested
        myService.execute(createRequestForSuccessResponse());

        // check how many times the ArgumentCaptor was called
        verify(mockAppender, times(1)).doAppend(loggingEventCaptor.capture());
        // get the reference to the LoggingEvent you want to inspect
        LoggingEvent loggingEvent = loggingEventCaptor.getAllValues().get(0);
        // check the logged message
        assertEquals("Webservice was successfully called", loggingEvent.getMessage());
        // check the log level
        assertEquals(Level.INFO, loggingEvent.getLevel());
    }

I hope that when you face this special situation, this may eventually help! ;-)

from: https://dev.to//claudiohigashi/mocking-logger-in-java-with-mockito-51k8

Published 0 original articles · liked 0 · visits 404

Guess you like

Origin blog.csdn.net/cunxiedian8614/article/details/105690128