Yes, that's certainly clearer, thank you.
Just to in case others with the same problem stumbles upon this thread,
here's the new improved version the test:
/**
* Tests that closeChannels() tries to close all the supplied Channels,
* even if supplied with nulls and Channels which fail to close.
*/
@Test
public void testCloseChannels() {
// Capture log entries from FileUtil
Logger logger = (Logger) LoggerFactory.getLogger(FileUtil.class);
logger.setAdditive(false);
ListAppender<LoggingEvent> listAppender =
new ListAppender<LoggingEvent>();
listAppender.start();
logger.addAppender(listAppender);
// Close a mix of normal, broken and completely missing channels
closeChannelse(null, new UnclosableChannelStub(),
null, new ChannelStub(), new ChannelStub());
// Restore normal logging
logger.detachAppender(listAppender);
logger.setAdditive(true);
// Perform checks
assertEquals("Exactly 1 failure should have been logged.", 1,
listAppender.list.size());
}
Ceki Gulcu-2 wrote:
>
>
> Hello Claus,
>
> A slightly easier way would be to use a ListAppender, which appends each
> event
> into a ...list. Logback-core has an implementation of about 10 lines each
> which
> is extensively for testing purposes in the other modules.
>
> Here it is:
>
> package ch.qos.logback.core.read;
>
> import java.util.ArrayList;
> import java.util.List;
>
> import ch.qos.logback.core.AppenderBase;
>
> public class ListAppender<E> extends AppenderBase<E> {
>
> public List<E> list = new ArrayList<E>();
>
> protected void append(E e) {
> list.add(e);
> }
> }
>
> Check the tests where ListAppender is used. I think BasicLoggerTest should
> give
> you a good idea of usage.
>
>
>
> HTH,
>
> Claus Nielsen wrote:
>> I have a unit test which checks that a class writes the log statements
>> it's supposed to.
>>
>> The following code works, but I was wondering if there isn't a better or
>> easier way to do this.
>>
>> Here's what I have now:
>>
>> public void testCloseChannels() {
>> Logger rootLogger = (Logger)
>> LoggerFactory.getLogger(
>> LoggerContext.ROOT_NAME);
>> Appender<LoggingEvent> appender =
>> rootLogger.getAppender("console");
>> appender.addFilter(new Filter() {
>> @Override
>> public FilterReply decide(Object o) {
>> LoggingEvent event = (LoggingEvent) o;
>> if (event.getMessage().equals("Failed to close channel.")) {
>> expectedLogEntryCount.incrementAndGet();
>> return FilterReply.DENY;
>> }
>> return FilterReply.NEUTRAL;
>> }
>> });
>> close(null, new UnclosableChannelStub(), null,
>> new ChannelStub(), new ChannelStub());
>> assertEquals("Exactly 1 failure should have been logged.",
>> 1, expectedLogEntryCount.intValue());
>> appender.clearAllFilters();
>> }
>>
>>
>> Regards,
>> Claus Nielsen
>
> --
> --
> Ceki Gülcü
> QOS.ch is looking to hire talented developers located in Switzerland.
> If interested, please contact c e k i AT q o s . c h
> _______________________________________________
> Logback-user mailing list
> [email protected]
> http://qos.ch/mailman/listinfo/logback-user
>
>
--
View this message in context:
http://www.nabble.com/Test-logging-in-unittest-tp18388101p18413915.html
Sent from the Logback User mailing list archive at Nabble.com.
_______________________________________________
Logback-user mailing list
[email protected]
http://qos.ch/mailman/listinfo/logback-user