Not seeing the log output from testing Service

321 Views Asked by At

I have a working Service (I know it works, because I manually tested). However, I am trying to build JUnit/Espresso test case for it. Though Android Studio told me that the test "passed, " but I don't see the effect from invoking the service. I tried to put some debugging message ,"Starting the service", but **I don't see any of the log message on the console.

Does anyone have any idea? Thanks!

private static StringBuilder log = new StringBuilder();

@Test
public void testWithoutLocation(){
    Intent intent = new Intent(InstrumentationRegistry.getContext(), SendMessage.class);

    intent.putExtra(BaseService.TAG_PERSON_ID, "ABCD");
    intent.putExtra(BaseService.TAG_MESSAGE_ID, "12345");

    try {
        log.append("Starting the service");
        oServiceTestRule.startService(intent);


    } catch (TimeoutException e) {
        e.printStackTrace();
        fail("TimeoutException caught");
    }

}
1

There are 1 best solutions below

1
NamNH On

With your current snip, I saw only one issue that you forget to show the log. So, make sure your test is run (passed as you said) and your logcat is activated (and select verbose level). So, try to change this:

try {
        log.append("Starting the service");
        oServiceTestRule.startService(intent);
        Log.d("YOUR_TAG", "testWithoutLocation: " + log.toString())

    } catch (TimeoutException e) {
        e.printStackTrace();
        fail("TimeoutException caught");
    }

If it's still not working, try to restart logcat or add more information.