Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Created July 11, 2011 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-neck/1075531 to your computer and use it in GitHub Desktop.
Save mike-neck/1075531 to your computer and use it in GitHub Desktop.
Invoking android.test.AndroidTestCase#getTestContext method
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.test.AndroidTestCase;
public class AndroidExtendedTestCase extends AndroidTestCase {
public Context getTestContext(){
Context context = null;
@SuppressWarnings("unchecked")
Class<AndroidExtendedTestCase> clz = (Class<AndroidExtendedTestCase>) this.getClass();
try {
Method method = clz.getMethod("getTestContext");
context = (Context) method.invoke(this);
} catch (SecurityException e) {
fail();
} catch (NoSuchMethodException e) {
fail();
} catch (IllegalArgumentException e) {
fail();
} catch (IllegalAccessException e) {
fail();
} catch (InvocationTargetException e) {
fail();
}
return context;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment