Skip to content

Commit

Permalink
Merge pull request #4 from LachlanMcKee/feature/add-throwable-logging…
Browse files Browse the repository at this point in the history
…-test

Added unit test to ensure throwables are logged
  • Loading branch information
LachlanMcKee authored Nov 19, 2017
2 parents 44541a1 + 4eeae3f commit ab0810e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ static void log(LogType logType, String message) {
}
}

static void log(LogType logType, String message, Throwable throwable) {
switch (logType) {
case VERBOSE:
Timber.v(throwable, message);
break;

case DEBUG:
Timber.d(throwable, message);
break;

case INFO:
Timber.i(throwable, message);
break;

case WARN:
Timber.w(throwable, message);
break;

case ERROR:
Timber.e(throwable, message);
break;
}
}

enum LogType {
VERBOSE, DEBUG, INFO, WARN, ERROR
}
Expand Down
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;
}
}
}

0 comments on commit ab0810e

Please sign in to comment.