Zachi
Zachi

Reputation: 231

Unit Test Android Service

I am trying to test my Service using "ServiceTestCase" framework as following:

@RunWith(AndroidJUnit4.class)
public class testTts extends ServiceTestCase<TtsLbEngine> {

    public testTts() {
        super(TtsLbEngine.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }


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

    }
}

The problem is that the service is Null. Can you please advise.

Upvotes: 0

Views: 617

Answers (1)

Zachi
Zachi

Reputation: 231

I managed to solve the problem. Please find below how to do it:

public class testService extends ServiceTestCase<myService> {


    public testService() {
        super(myService.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }


    @Test
    public void testService() throws Exception {
        Intent startIntent = new Intent();

        startIntent.setClass(getContext(), myService.class);
        startService(startIntent);

        myService s = getService();

        assertNull(s);
    }
}

Upvotes: 1

Related Questions