5

I'm having issues creating unit tests for a Service in Android Studio. I have set up my project to perform unit tests and have successfully set up tests for a different (non-Service) class. I can run those tests and have them pass.

This is the code for 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> {

    /** Tag for logging */
    private final static String TAG = GeneralServiceTest.class.getName();

    public GeneralServiceTest() {
        super(GeneralService.class);
    }
//
    @Before
    public void setUp() throws Exception {
        super.setUp();

        Log.d(TAG, "Setup complete");
    }

    @After
    public void tearDown() throws Exception {
        Log.d(TAG, "Teardown complete");


        super.tearDown();
    }

    @Test
    public void testOnCreate() throws Exception {
        Intent intent = new Intent(getSystemContext(), GeneralService.class);
        startService(intent);
        assertNotNull(getService());

    }

}

As you can see it is about as simple as I can make it. It does not matter what I try, assertNotNull always fails.

From debugging the test, I have found that the intent created with:

Intent intent = new Intent(getSystemContext(), GeneralService.class);

always returns null.

I have tried setting the context and class like so

    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'm out of ideas. There are very few examples for ServiceTestCase that aren't years out of date and of those I have found (such as here: http://alvinalexander.com/java/jwarehouse/android-examples/samples/android-9/ApiDemos/tests/src/com/example/android/apis/app/LocalServiceTest.java.shtml) I have tried to copy their code as closely as possible and have had no success.

I theorised that I might need to have the unit test run on a device or an emulator, but this wasn't necessary for my other unit tests that succeeded.

In summary, Why is my attempt to create an intent always returning null?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
James Dobson
  • 1,191
  • 2
  • 8
  • 14
  • I have the same issue, but in the example you've copied from they clearly state: * To run this test, you can type: * adb shell am instrument -w \ * -e class com.example.android.apis.app.LocalServiceTest \ * com.example.android.apis.tests/android.test.InstrumentationTestRunner which indicates that they run it on an emulator or a device. I'm actually curious how you get other unit tests to run without an emulator, were they basic Junit tests or android tests? – Nonos Jul 01 '15 at 22:01
  • Your service might not be started by the time your test case is running. I've searched a lot and couldn't find many samples on Service testing. But [this](http://ics.upjs.sk/~novotnyr/blog/1160/one-does-not-simply-test-the-intentservices) helped me to understand the concept behind testing services. – Prudhvi Aug 11 '15 at 20:58
  • 1
    As it turns out, I was misunderstanding the concept - I was running instrumentation tests but not on a device, which is why I couldn't get services running. In order to test services I needed to set up and run instrumentation tests which required deployment to a device. – James Dobson Aug 25 '15 at 12:14
  • 1
    ATTENTION! My answer was deleted, because it was "duplicate", let's try a comment. ServiceTestCase is deprecated and not working as expected. https://google.github.io/android-testing-support-library/docs/rules/ – Herrbert74 Jun 07 '16 at 09:42

0 Answers0