0

How do I test that my activity started a service with a particular intent?

My question is similar to Unit testing Activity.startService() call, but I ran into an action bar problem when I used ActivityUnitTestCase.

Here is an example of what I would like to test:

@UiThreadTest
public void testAddSongToAvailableList() {
    ListView songList = (ListView) this.getActivity().findViewById(R.id.allSongsList);
    songList.getChildAt(0).findViewById(R.id.btnAddToAvailableList).performClick(); // should start my service

    //assert that my service was started and is doing stuff
}
Community
  • 1
  • 1
MageWind
  • 785
  • 1
  • 6
  • 16

2 Answers2

0

pass in the data as a bundle to the initiating activity?

Are you simple testing the service is started or do you want to specifically know which activity initiated the service?

reidisaki
  • 1,525
  • 16
  • 29
  • Ideally I want to test that the service received the activity's intent. Something like an ActivityMonitor would work nicely, but I don't know of one for services. – MageWind Aug 13 '13 at 23:39
0

When you start a service using startService method, you can determine if the activity has started or not by return value of the method.

If service started successfully, it returns the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.

For more detail: Android Documentation

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67