0

I'm trying to test an external binding to a Service I've defined. Following the class I'm using for instrumented test:

@Rule
public final ServiceTestRule serviceRule = new ServiceTestRule();

@Test
public void testWithBoundService() throws TimeoutException, RemoteException {
    IMyInterface iMyInterface;
    Intent serviceIntent =
            new Intent(ApplicationProvider.getApplicationContext(),
                    MyService.class);
    IBinder binder = serviceRule.bindService(serviceIntent);
    assertNotNull(binder);
    iMyInterface = IMyInterface.Stub.asInterface(binder);
    assertTrue(iMyInterface.retrieveValue(new Attribute()).getValues().get(0).equals("home"));
}

@Test
public void testWithBoundServiceExternal() throws TimeoutException, RemoteException {
    IMyInterface iMyInterface;
    Intent serviceIntent = new Intent();
    serviceIntent.setClassName("a.b.c.d", "a.b.c.d.MyService");
    IBinder binder = serviceRule.bindService(serviceIntent);
    assertNotNull(binder);
    iMyInterface = IMyInterface.Stub.asInterface(binder);
    boolean reachedHere = false;

    assertTrue(iMyInterface.retrieveValue(new Attribute()).getValues().get(0).equals("home"));

}

First Test function runs without any errors, the second fails with the following message:

Failed to bind to service! Is your service declared in the manifest?

The Service is defined in the package a.b.c.d, the same of the Aidl interface, while the instrumentedTest is running in the a.b.c.e package

Antonio La Marra
  • 5,949
  • 4
  • 15
  • 23

1 Answers1

0

Few minutes later I realized that the Manifest had a different package, and, in order to call the Service you should use the package of the Manifest. So suppose that you hava, in the tag an attribute package="a.b.c.f", in order to call an external service you should use:

serviceIntent.setClassName("a.b.c.f", "a.b.c.d.MyService") 
Antonio La Marra
  • 5,949
  • 4
  • 15
  • 23
  • wish it worked for me. I still get a null pointer exception (my service is still not found and not started) – Brian Reinhold Jul 06 '19 at 18:05
  • does the first test work in your case? Can you also share the structure of your project? – Antonio La Marra Jul 08 '19 at 07:15
  • Antonio La Mara: After struggling for a couple of days with this and trying all the different solutions with none working, I posted a question here https://stackoverflow.com/questions/56922254/androidx-servicetestrule-cannot-find-my-service – Brian Reinhold Jul 08 '19 at 11:05
  • 1
    @BrianReinhold I've looked your question, have you tried not using thes lines? serviceRule.startService(intent); Thread.sleep(12000); – Antonio La Marra Jul 08 '19 at 11:26
  • Antonio yes; those lines were added later. Didn't make any difference. (The '@' thing is not working for me) – Brian Reinhold Jul 08 '19 at 15:48