简体   繁体   中英

Mockito how to capture multiple logEvent object for consecutive calls to same method with log4j2.18

I'm using Mockito to verify that my log is sending the correct message. However, the test is coming as incorrect, because the log event is mutable so when I'm making consecutive call to the same method it just returns the recent value, not 2 different value. As a result, I'm trying to return a LogEvent object OR append the event before it gets capture.

IE what is happening is that when I verify and append the capture

verify(mockAppender, times(2)).doAppend(loggingEventCaptor.capture());
List<LoggingEvent> capturedLoggingEvents = loggingEventCaptor.getAllValues();
capturedLoggingEvents.get(0).message // this is printing "Test 2" but should be "Test 1"
capturedLoggingEvents.get(1).message // this is printing "Test 2" 

the loggingEventCaptor.messages are the same. "Test 2", "Test 2" but I want it to have "Test 1" and "Test 2: for my junit 4 test cases.

I tried creating a spy but and a when command but I'm getting invalid can someone assist

LoggingEvent logEvent = Mockito.spy(loggingEventCaptor.capture());
when(mockAppender.doAppend(loggingEventCaptor.capture())).thenReturn(LogEvent);
verify(mockAppender, times(2)).doAppend(loggingEventCaptor.capture());

but I'm getting errors. Basically I want the loggingEventCaptor to contain a separate LogEvent object, instead of 1 mutable one. So I can get "Test1" and "Test 2" instead of "Test 2" and "Test 2" Or an alternative is to store all those events when it's changing the mutable one but I'm not sure how to do that. Can someone please guide thanks. I'm using log4j1 imports but I have a 1-2Log4j bridge.

EDIT: so in debugger mode it will return separate object ID LogEventAdaptor@123 LogEventAdaptor@124 but inside the apdator it has MutableLogEvent@124 MutableLogEvent@124 with the message from LogEventAdaptor@124. Which is why testcase is failing

To achieve the behavior you want, you can use the ArgumentCaptor class provided by Mockito. This class allows you to capture arguments passed to a mocked method, and then verify their values. Here's an example of how you can use it in your test:

// Create an ArgumentCaptor to capture LoggingEvent objects
ArgumentCaptor<LoggingEvent> loggingEventCaptor = ArgumentCaptor.forClass(LoggingEvent.class);

// Verify that the doAppend method is called twice, and capture the LoggingEvent objects
verify(mockAppender, times(2)).doAppend(loggingEventCaptor.capture());

// Get the list of captured LoggingEvent objects
List<LoggingEvent> capturedLoggingEvents = loggingEventCaptor.getAllValues();

// Verify the values of the captured LoggingEvent objects
assertEquals("Test 1", capturedLoggingEvents.get(0).message);
assertEquals("Test 2", capturedLoggingEvents.get(1).message);

In this code, the loggingEventCaptor object captures the LoggingEvent objects passed to the doAppend method when it's called. Then, you can use the getAllValues method of the loggingEventCaptor object to get a list of all captured LoggingEvent objects. Finally, you can verify the values of the captured objects using assertions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM