2

So, I'm new to android unit testing. I'm trying to write a unit test for the Phone application:

package com.android.phone;

import android.content.Intent;
import android.net.Uri;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.MediumTest;

import com.android.phone.PhoneApp;

import dalvik.annotation.TestTargetClass;

@TestTargetClass(PhoneApp.class)
public class TestPhone extends ApplicationTestCase<PhoneApp> {

        public TestPhone() {
                super(PhoneApp.class);
        }

        private PhoneApp phone;

        @Override
        protected void setUp() throws Exception {
                super.setUp();
                phone = getApplication();
        }

        @MediumTest
        public void testDialerIsUp() {
                assertNotNull("Phone app does not exist", phone);
                // TODO add tests
        }

}

Then I start an emulator, wait till it boots up, and run those tests:

adb shell am instrument -e class com.android.phone.TestPhone -r -w com.android.phone.tests/android.test.InstrumentationTestRunner

And now I'm getting a junit.framework.AssertionFailedError: PhoneApp does not exist. What is wrong here, why isn't PhoneApp up?

4 Answers 4

2

Actually, I'd recommend calling createApplication() in your setUp() method before calling phone = getApplication().

Sign up to request clarification or add additional context in comments.

Comments

0

You don't show the code for your PhoneApp. Did you derive a PhoneApp class from the android.app.Application class? Or are you expecting that there is just something called PhoneApp out there that you can test?

You will need to write an android.app.Application class as part of your project, if you expect to test something.

Or, perhaps, you are talking about something that I do not understand. That is always possible.

2 Comments

This app is a part of android platform.
0

How does this even compile with "PhoneApp.class" in it if you just stick to the SDK?

I know you can use Robotium to test existing apps though.

1 Comment

I don't just stick to the SDK. I've got the whole platform sources.
0

Elaborating on Karim's answer (it does work), this is the setup method:

    MyApplication application;

    @Override
    protected void setUp() throws Exception {
            super.setUp();
            createApplication();
            application = getApplication();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.