Skip to content

Instantly share code, notes, and snippets.

@westonal
Created March 9, 2017 13:47
Show Gist options
  • Select an option

  • Save westonal/f6043d66d96e854c5798d388e89112a0 to your computer and use it in GitHub Desktop.

Select an option

Save westonal/f6043d66d96e854c5798d388e89112a0 to your computer and use it in GitHub Desktop.
Timber issue

Logging with a throwable calls Log and you can't stop it

Timber 3.1.0

uprootAll disables the logging, this works with, say Timber.w(String) but not Timber.w(Throwable, String). Here is a test that demonstrates this when run without Robolectric:

import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

import timber.log.Timber;

import static org.junit.Assert.assertEquals;

public final class TimberProblem {

    @Test
    public void OK() {
        final AtomicInteger i = new AtomicInteger();
        Timber.uprootAll();
        Timber.plant(new Timber.Tree() {
            @Override
            protected void log(int priority, String tag, String message, Throwable t) {
                i.incrementAndGet();
            }
        });
        Timber.w("Test");
        assertEquals(1, i.get());
    }

    @Test
    public void Problem() {
        final AtomicInteger i = new AtomicInteger();
        Timber.uprootAll();
        Timber.plant(new Timber.Tree() {
            @Override
            protected void log(int priority, String tag, String message, Throwable t) {
                i.incrementAndGet();
            }
        });
        Timber.w(new Exception(), "Test");  //java.lang.RuntimeException: Method getStackTraceString in android.util.Log not mocked.
        assertEquals(1, i.get());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment