idunno
idunno

Reputation: 713

Getting a Context for use in AndroidTestCase when class under test is not an activity

I am trying to test using AndroidTestCase. I am trying to test only one particular class in my application, however this class does not extend Activity, Service or anything else. It is basically a plain Java class apart from the fact that it requires a Context. It is a pojo and some of its variables are objects that require android api calls in their creation, e.g. a call to the SensorManager. I tried to use:

Context context = getContext();

When running my tests this gives me the exception "System services not available to activites before onCreate()". Does that method have to be overridden? final Context context = new IsolatedContext(null, getContext()) gives the same thing.

The reason I am using the Android testing framework and not something like Robolectric is because the class I'm testing gathers hardware information about a device and so I want to run the tests on an actual device. I have looked at the developer docs for AndroidTestCase but can't see what I'm looking for in the examples. I'm not sure the other test case classes will achieve what I want. Any ideas?

My test class:

public class DeviceTest extends AndroidTestCase {
ClassToTest mClassToTest;

@Override
protected void setUp() throws Exception {
final Context context = new IsolatedContext(null, getContext()) {
        @Override
        public Object getSystemService(final String pName) {
            return getContext().getSystemService(pName);
        }
    };

mClassToTest = new ClassToTest(context);
 super.setUp();
}

public void testClassMethod() {
 Object mObject;
 mObject = mClassToTest.getObject();
 assertNotNull(mObject);
}

@Override
protected void tearDown() throws Exception {
 mClassToTest = null;
 super.tearDown();
}
}

Thanks in advance.

UPDATE: After changing my setup to the following:

 @Override
 protected void setUp() throws Exception {
 super.setUp();
 context = this.getContext();
 mClassToTest = new ClassToTest(context);
}

I am getting an error that context is null. In what scenarios would AndroidTestCase.getContext() return null? My setup seems to be ok....

Upvotes: 4

Views: 4735

Answers (3)

Vijay Vankhede
Vijay Vankhede

Reputation: 3058

You can use mContext from super class (AndroidTestCase). I used it for the testing of the database where context is required.

AndroidTestCase.class

public class AndroidTestCase extends TestCase {
    protected Context mContext;
    ...
}

You would be able to use Context in the inherited class of AndroidTestCase.

TestDb.java

public class TestDb extends AndroidTestCase {
    void deleteTheDatabase() {mContext.deleteDatabase(DB_NAME)};
}

Upvotes: 1

Andrew Mackenzie
Andrew Mackenzie

Reputation: 5745

From AndroidTestCase you can access directly mContext, or call getContext().

From the context returned by those, you could also call Context.getApplicationContext() if you wanted that one.

Upvotes: 1

Paul Harris
Paul Harris

Reputation: 5819

There are a few ways around this, you could use a mockcontext as one solution or if you really do not care what the context is just that is valid you can use an InstrumentationTestCase and get the context of the test apk via getInstrumentation().getContext().

I think the reason your context is null is that actually no android context exists at this point, you can get one by creating an application or an activity.

Upvotes: 0

Related Questions