All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mediamiser.test.Log4j1ErrorAppender Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
// Copyright (c) 2013-2014 MediaMiser Ltd. All rights reserved.
package com.mediamiser.test;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;

/**
 * A log4j1 extension that will throw an {@code java.lang.AssertionError} if a
 * {@code Level.ERROR} logging statement is propagated to an slf4j logger.
 * 
 * 
*
* This is used to detect whether an error condition is met during the course of * unit tests. If a test causes production code to log an error, it will fail * the test by throwing an {@code java.lang.AssertionError}. For code coverage * reasons, tests still need to ensure that when an error condition occurs that * a {@code Level.ERROR} logging statement is propagated to an slf4j logger; * this can be accomplished using {@link com.mediamiser.test.LogTester}. * *
*
* To use this appender in your test suite, add it to the root logger before any * tests run, e.g., before your test suite is run: * *
*
* *
 * public class MyTestSuite {
 * 
 * 	@BeforeClass
 * 	public static void failTestWhenItLogsAnError() {
 * 		Logger.getRootLogger().addAppender(new Log4j1ErrorAppender());
 * 	}
 * 
 * }
 * 
* * @author Chris Fournier */ public class Log4j1ErrorAppender extends AppenderSkeleton { @Override public void close() { // Do nothing } @Override public boolean requiresLayout() { return false; } @Override protected void append(final LoggingEvent event) { // If an error was logged, throw an assertion so that the test that // caused the error will fail if (event.getLevel() == Level.ERROR) { throw new AssertionError("Test logged at the error level: " + event.getMessage()); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy