Package com.google.common.testing
Class TestLogHandler
- java.lang.Object
-
- java.util.logging.Handler
-
- com.google.common.testing.TestLogHandler
-
@Beta @GwtCompatible public class TestLogHandler extends java.util.logging.Handler
Tests may use this to intercept messages that are logged by the code under test. Example:TestLogHandler handler; protected void setUp() throws Exception { super.setUp(); handler = new TestLogHandler(); SomeClass.logger.addHandler(handler); addTearDown(new TearDown() { public void tearDown() throws Exception { SomeClass.logger.removeHandler(handler); } }); } public void test() { SomeClass.foo(); LogRecord firstRecord = handler.getStoredLogRecords().get(0); assertEquals("some message", firstRecord.getMessage()); }- Since:
- 10.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<java.util.logging.LogRecord>listWe will keep a private list of all logged records
-
Constructor Summary
Constructors Constructor Description TestLogHandler()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()voidclose()voidflush()java.util.List<java.util.logging.LogRecord>getStoredLogRecords()Returns a snapshot of the logged records.voidpublish(java.util.logging.LogRecord record)Adds the most recently logged record to our list.
-
-
-
Method Detail
-
publish
public void publish(java.util.logging.LogRecord record)
Adds the most recently logged record to our list.- Specified by:
publishin classjava.util.logging.Handler
-
flush
public void flush()
- Specified by:
flushin classjava.util.logging.Handler
-
close
public void close()
- Specified by:
closein classjava.util.logging.Handler
-
clear
public void clear()
-
getStoredLogRecords
public java.util.List<java.util.logging.LogRecord> getStoredLogRecords()
Returns a snapshot of the logged records.
-
-