Service Testing

1,521 views
Skip to first unread message

Ian

unread,
May 7, 2012, 4:07:42 PM5/7/12
to Robolectric
Hi folks,

Robolectric seems great! I'm fumbling around with my first attempt -
testing an Android service. My first step is to try testing the
binding process, but the framework calls my ServiceConnection callback
with null arguments (hence the assetNotNull() line below always
fails). Can someone please guide me. Heres my code....

@RunWith(RobolectricTestRunner.class)
public class MyServiceTest {

@Test
public void testServiceBinding() throws Exception {
Context context = new MyActivity();
Intent serviceIntent = new Intent(context, MyService.class);
serviceIntent.setClassName(context,
MyActivity.class.getSimpleName());
boolean ok = context.bindService(serviceIntent, conn,
Context.BIND_AUTO_CREATE);
Assert.assertTrue(ok);
Thread.sleep(10); // Perhaps we dont need?
Assert.assertNotNull(binder);
}

private ServiceConnection conn = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName arg0) { }

@Override
public void onServiceConnected(ComponentName comp, IBinder b) { //
Both args are null
binder = b;
}
};

private IBinder binder;

}

Many Thanks for any Help
Ian Hunter



Aaron VonderHaar

unread,
May 13, 2012, 1:04:22 PM5/13/12
to robol...@googlegroups.com
Your code looks correct. Are you still having problems with this?

You shouldn't need the Thread.sleep(10) that you have there.

Can you check if shadowOf(context).peekNextStartedService() returns
conn after the call to bindService().

If you have the source of Robolectric attached, maybe set a breakpoint
at your onServiceConnected and check the stack trace in the Shadow*
classes to make sure the parameters you are expecting are being passed
down.

Cheers,
--Aaron V.
--
--Aaron V.
[ http://github.com/avh4 ]

arok

unread,
Jul 15, 2012, 3:24:11 PM7/15/12
to robol...@googlegroups.com
I have same problem. Any solution?

вторник, 8 мая 2012 г., 0:07:42 UTC+4 пользователь Ian Hunter написал:

gho...@pivotallabs.com

unread,
Mar 5, 2014, 2:51:15 PM3/5/14
to robol...@googlegroups.com
Old thread, but if anyone has the same issue try:
  shadowOf(Roblectric.application).setComponentNameAndServiceForBindService(name, binder);

Veeren Mandalia

unread,
May 19, 2015, 8:58:07 PM5/19/15
to robol...@googlegroups.com
Does anyone have the issue where ServiceConnection.onServiceDisconnected(ComponentName) is called after a call to Context.unbindService()?

Android documentation states that onServiceDisconnected will be called when the process hosting the service has crashed or been killed and not on unbindService().

I am using: testCompile('org.robolectric:robolectric:3.0-rc2') with Android Studio 1.2.1.1

I call setComponentNameAndServiceForBindService before binding to receive the onServiceConnected(...) callback:
shadowOf(RuntimeEnvironment.application).setComponentNameAndServiceForBindService(
new ComponentName("some.package.name", "IMyService"), mock(IBinder.class));
Reply all
Reply to author
Forward
0 new messages