Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceTestCase<T>.getService?

I'm trying to build a valid junit testing suite on android.

Since i'm new to Junit i can't figure out how to use the ServiceTestCase class.

I can't figure it out how to get the getService() method working. it ever returns me null . So i decided to start it via startService. It does not work.

Could you please help me ?

Thanks

like image 476
Andrea Baccega Avatar asked Feb 19 '10 22:02

Andrea Baccega


1 Answers

This is what you need to test your service

public class MyServiceTests extends ServiceTestCase<MyService> {

private static final String TAG = "MyServiceTests";

public MyServiceTests() {
    super(MyService.class);
}

/**
 * Test basic startup/shutdown of Service
 */
@SmallTest
public void testStartable() {
    Intent startIntent = new Intent();
    startIntent.setClass(getContext(), MyService.class);
    startService(startIntent);
    assertNotNull(getService());
}

/**
 * Test binding to service
 */
@MediumTest
public void testBindable() {
    Intent startIntent = new Intent();
    startIntent.setClass(getContext(), MyService.class);
    IBinder service = bindService(startIntent);
    assertNotNull(service);
}
}

I've written some articles about Android testing and test driven development that you may find useful, check http://dtmilano.blogspot.com/search/label/test%20driven%20development.

like image 136
Diego Torres Milano Avatar answered Sep 22 '22 16:09

Diego Torres Milano