-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from LachlanMcKee/feature/add-throwable-logging…
…-test Added unit test to ensure throwables are logged
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...-junit-sample/src/test/java/net/lachlanmckee/timberjunit/sample/LogTestWithThrowable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package net.lachlanmckee.timberjunit.sample; | ||
|
||
import net.lachlanmckee.timberjunit.TimberTestRule; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.OutputStream; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
@RunWith(Parameterized.class) | ||
public class LogTestWithThrowable { | ||
private static final String EXCEPTION_OUTPUT = "net.lachlanmckee.timberjunit.sample.LogTestWithThrowable$NoStackTraceThrowable"; | ||
|
||
@Rule | ||
public TimberTestRule mTimberTestRule = TimberTestRule.builder() | ||
.showThread(false) | ||
.showTimestamp(false) | ||
.onlyLogWhenTestFails(false) | ||
.build(); | ||
|
||
@Parameterized.Parameters | ||
public static Collection<Object[]> data() { | ||
Throwable foo = new NoStackTraceThrowable(); | ||
return Arrays.asList(new Object[][]{ | ||
{LogTester.LogType.VERBOSE, foo, "Test", "V/LogTester: Test\n" + EXCEPTION_OUTPUT}, | ||
{LogTester.LogType.DEBUG, foo, "Test", "D/LogTester: Test\n" + EXCEPTION_OUTPUT}, | ||
{LogTester.LogType.INFO, foo, "Test", "I/LogTester: Test\n" + EXCEPTION_OUTPUT}, | ||
{LogTester.LogType.WARN, foo, "Test", "W/LogTester: Test\n" + EXCEPTION_OUTPUT}, | ||
{LogTester.LogType.ERROR, foo, "Test", "E/LogTester: Test\n" + EXCEPTION_OUTPUT} | ||
}); | ||
} | ||
|
||
private final LogTester.LogType mLogType; | ||
private final Throwable mThrowable; | ||
private final String mMessage; | ||
private final String mExpectedOutput; | ||
|
||
public LogTestWithThrowable(LogTester.LogType logType, Throwable throwable, String message, | ||
String expectedOutput) { | ||
mLogType = logType; | ||
mThrowable = throwable; | ||
mMessage = message; | ||
mExpectedOutput = expectedOutput; | ||
} | ||
|
||
@Test | ||
public void givenOutputStreamSetup_whenLogExecuted_thenVerifyExpectedOutput() { | ||
// given | ||
OutputStream outputStream = LogTesterTestUtils.setupConsoleOutputStream(); | ||
|
||
// when | ||
LogTester.log(mLogType, mMessage, mThrowable); | ||
|
||
// then | ||
LogTesterTestUtils.assertOutput(outputStream, mExpectedOutput); | ||
} | ||
|
||
private static final class NoStackTraceThrowable extends Throwable { | ||
@Override | ||
public synchronized Throwable fillInStackTrace() { | ||
return this; | ||
} | ||
} | ||
} |