3

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?

Mellon
  • 37,586
  • 78
  • 186
  • 264
  • I don't think i will be able to answer this :(, we need someone more experienced. – Rachit Mishra Sep 10 '13 at 11:33
  • I think you have forgot to mention the service name in the Manifest file. In such a case you are neither reported by the system about the fault commited . – Khay May 24 '15 at 19:20

3 Answers3

1

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 ?

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
  • But my test case is not meant to test my service that's why I don't use service specific test framework. I am testing something else, but I would like to have my service be running to do some work at the meantime. – Mellon Sep 10 '13 at 11:39
0

If you want to wowk with service then use ServicetestCase.

Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • MyTest.this is not a context instance, you can't do that. and YES I have onBind in my service class. – Mellon Sep 10 '13 at 11:44
  • use mContext=getApplicationContext or getBaseContext(). May be it will work. – Pratik Dasa Sep 10 '13 at 11:45
  • pratik, thank you for your answer but have you used AndroidTestCase before? Could you please show me how can I call getApplicationContext or getBaseContext in AndroidTestCase class?? There is only getContext() method available in this class – Mellon Sep 10 '13 at 11:47
  • But why are you using AndroidTestdase, is there any requirement for it?? – Pratik Dasa Sep 10 '13 at 11:51
  • You can use ServiceTestCase to use Service. – Pratik Dasa Sep 10 '13 at 11:54
  • check this link : http://stackoverflow.com/questions/6604613/can-androids-servicetestcasemyservice-send-messages-to-my-service – Pratik Dasa Sep 10 '13 at 11:57
0

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 ...

Andy Newman
  • 1,178
  • 2
  • 8
  • 23