I am having trouble creating unit tests for a service in Android Studio. I created my project to run unit tests and successfully configured tests for another (non-service) class. I can run these tests and skip them.
This is the code of my ServiceTestCase:
package com.roche.parkinsons.service; import android.content.Intent; import android.test.ServiceTestCase; import android.util.Log; import org.junit.After; import org.junit.Before; import org.junit.Test; public class GeneralServiceTest extends ServiceTestCase<GeneralService> { private final static String TAG = GeneralServiceTest.class.getName(); public GeneralServiceTest() { super(GeneralService.class); }
As you can see, this is about as simple as I can do it. No matter what I try, assertNotNull always fails.
From debugging the test, I found that the intent was created using
Intent intent = new Intent(getSystemContext(), GeneralService.class);
always returns null.
I tried setting the context and class as follows
public void testOnCreate() throws Exception { Intent intent = new Intent(getSystemContext(), GeneralService.class); intent.setClass(getSystemContext(), GeneralService.class); startService(intent); assertNotNull(getService()); }
But this has no effect.
I have no ideas. There are very few examples for ServiceTestCase that are not outdated and those that I found (for example, here: http://alvinalexander.com/java/jwarehouse/android-examples/samples/android-9/ApiDemos/tests/src/com /example/android/apis/app/LocalServiceTest.java.shtml ) I tried to copy their code as close as possible and were unsuccessful.
I suggested that I might need to run the unit test on a device or emulator, but this was not necessary for my other unit tests that succeeded.
So why is my attempt to create an intent always returning null?