Reputation: 38842
I have a test project, my simple test case extends AndroidTestCase
class :
public class MyTest extends AndroidTestCase{
private Context mContext;
public MyTest(){
super();
}
@Override
public void setUp() throws Exception {
super.setUp();
mContext = getContext();
//Start my service
mContext.startService(new Intent(mContext, MyService.class));
}
@Override
protected void runTest() {
...
}
...
}
In setUp()
callback of my above test case, I started MyService
.
MyService has also been declared in AndroidManifest.xml of my test project:
<service
android:name="com.my.app.services.MyService"/>
MyService.java :
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d("MyService", "onStartCommand");
}
...
}
But after I run my test case, I noticed from log that neither onCreate()
nor onStartCommand()
callbacks of MyService
have been called.
Why? Is there any special rule applied to Service usage in Android Test Framework which I missed?
Upvotes: 3
Views: 1618
Reputation: 1208
I have been having the same problem, I think. I am trying to test code which calls startService, not test the service itself, so I created a new service for testing purposes.
It appears that test cases can start services, but the services have to be part of the main project being tested, they can't be part of the test project. I don't really like having a test only class in my project, but since it seems to be the only way ...
Upvotes: 0
Reputation: 10938
The context returned by AndroidTestCase is probably a mocked context - it probably has no implementation for startService. Have you read http://developer.android.com/tools/testing/service_testing.html ?
Upvotes: 1