I have simple ServiceTestCase and an empty IntentSerice containing life-cycle methods only, the Service is registered AndroidManifest.xml also, as well as an Action tag "com.tut.my.service.DATAUPDATE".
The calling of Service works pretty well, all live-cycle methods appears in order, but something strange happens after the finish of my custom test. ServiceTestCase issued the method testServiceTestCaseSetUpProperly() AFTER my custom test to ensure that setupService() runs correctly.
So I'm looking arround and find some interesting blog concern setupService(), but the conclusion was not satisfy. The blog author advises - for some good reason - not to call startService(...) in setUp(), as made himself abundantly clear why it's not a good idea though.
However, the problem that arose for me from calling testServiceTestCaseSetUpProperly() after my custom test is the call for a new instance of MyService service. This ends up invoking onCreate and than the Service dies for some reason, finito... but all tests passes successfully.
Here is the source for the Service:
public class MyService extends IntentService {
public static final String INTENT = "com.tut.my.service.DATAUPDATE";
public MyService() {
super(INTENT);
Log.d(getClass().getSimpleName(), "called: std c-tor()");
}
@Override
public void onCreate() {
super.onCreate();
Log.d(getClass().getSimpleName(), "called: onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(getClass().getSimpleName(), "called onStartCommand() --> intent: " + intent.getAction() + " flags: " + flags + " startID: " + startId);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(getClass().getSimpleName(),"called: onDestory()");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(getClass().getSimpleName(), "called: onHandleIntent() --> Intent: " + intent.getAction());
setPriceData();
}
}
Here is the source for ServiceTestCase
@MediumTest
public class MyServiceTest extends ServiceTestCase<MyService> {
Context mCtx = null;
public MyServiceTest() {
super(MyService.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mCtx = getSystemContext();
}
@MediumTest
public void testGetMyServiceData() throws InterruptedException {
Intent i = new Intent(MyService.INTENT);
Log.d(getClass().getSimpleName(), "startService(i)");
mCtx.startService(i);
// throttle the instrumentation thread so the service
// can be instantiated for sure
Log.d(getClass().getSimpleName(), "before Thread Sleep");
Thread.sleep(8000); // needs 10 seconds before ANR
Log.d(getClass().getSimpleName(), "after Thread Sleep");
}
}
And the corresponding (stripped) LogCat output:
I/TestRunner(11954): started: testGetMyServiceData(com.tut.my.service.MyServiceTest)
D/MyServiceTest(11954): startService(i)
D/MyService(11954): called: std c-tor()
D/MyServiceTest(11954): before Thread Sleep
D/MyService(11954): called: onCreate()
D/MyService(11954): called onStartCommand() --> intent: com.tut.my.service.DATAUPDATE flags: 0 startID: 1
D/MyService(11954): called: onHandleIntent() --> Intent: com.tut.my.service.DATAUPDATE
D/MyService(11954): called: onDestory()
D/MyServiceTest(11954): after Thread Sleep
I/TestRunner(11954): finished: testGetMyServiceData(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): passed: testGetHarvestData(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): started: testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
D/MyService(11954): called: std c-tor()
I/TestRunner(11954): finished: testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): passed: testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): started: testAndroidTestCaseSetupProperly(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): finished: testAndroidTestCaseSetupProperly(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): passed: testAndroidTestCaseSetupProperly(com.tut.my.service.MyServiceTest)
I/ActivityManager(71): Force stopping package com.tut.my.service uid=10036
I/Process(71): Sending signal. PID: 11954 SIG: 9
So the question is, why testServiceTestCaseSetupProperly() is called at the end and not in the first place? And why MyService object dies so ungracefully.
EDIT : To make it more precise:
The important part of my concern is the rude call to onCreate.
I/TestRunner(11954): started: testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
D/MyService(11954): called: std c-tor() <--- ONLY one call to onCreate
I/TestRunner(11954): finished:testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
JUnit 3 uses reflection to obtain the tests to run. It is not guaranteed that tests are run in any specific order. Additionally, unit tests should be independent of each other so their execution order should not be relevant.