2

I've read every question related to this and nothing is working for me.

  • Android studio version: 2.3.3
  • compileSdkVersion 25
  • buildToolsVersion '26.0.1'
  • Gradle build tools version: 2.3.3

I'm trying to do something very simple. When I run a test, I want to see the output of the test. Here's some sample code:

// MyClassTest
@Test
public void myMethodTest() throws Exception {
    System.out.println("Hello from myMethodTest!");
    int res = Whitebox.invokeMethod(MyClass.class, "myMethod", arg1, arg2);
    assertThat(res, is(1));
}

// MyClass
private int myMethod(int arg1, int arg2) {
    System.out.println("Hello from myMethod!");
    return 0;
}

The whitebox stuff is probably not important here but I included it just in case. When I run this all it tells me is that the assertion failed. I also get the "Hello from myMethodTest" output, but I can't see the "Hello from myMethod" output. This makes it extremely hard to figure out where my method is going wrong. None of the Timber stuff I'm logging appears either.

I've tried running the tests via both the GUI and the console with gradle and gradlew, with all the various options (--info, --stacktrace, etc.). I've also tried all the stuff here, here, and here. Surely this must be possible, thanks.

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44
  • Is this instrumentation testing (`androidTest/`) or unit testing (`test/`)? – CommonsWare Nov 20 '17 at 00:10
  • Unit testing. I'm using Powermockito if it helps also. I've tried sorting this out both on a Windows machine and a Mac machine, and I've tried upgrading to the 3.0.0 build tools, which also didn't help. It seems strange that the regular old stdout of a test is so hard to find. – Daniel Porteous Nov 20 '17 at 00:13
  • Hmmmm... off the cuff, it feels like `myMethod()` is simply not being called (presumably via reflection somewhere inside that `Whitebox` stuff). What evidence do you have that it *is* being called? – CommonsWare Nov 20 '17 at 00:15
  • Oh my god, what an insight. I can't believe I didn't think about testing this. I just made a regular public method in the equivalent of `MyClass` with a print statement and there it appeared. Such a simple thing, looking in the completely wrong place. Thanks a lot, I'm very grateful! – Daniel Porteous Nov 20 '17 at 00:20
  • It's also very good to know that Timber output does **not** appear in these unit tests. – Daniel Porteous Nov 20 '17 at 00:22

1 Answers1

3

Occam's Razor says that myMethod() is not being called. You're not getting the log output (while having evidence that logging elsewhere is working) and your assertion based on the result is failing.

It's also very good to know that Timber output does not appear in these unit tests

I haven't played with Timber in unit tests to know what it will do when there's no Android around with Log to use.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks a lot. I would have hopefully arrived at this conclusion too if I was confident that stdout was meant to appear in the test output, but I hadn't assured myself of that first. Thanks! – Daniel Porteous Nov 20 '17 at 00:25