You should use the InstrumentationTestCase
(and subclasses, depending on your needs).
You can get the Context
by calling getInstrumentation().getContext()
inside your test cases.
When using Gradle you just need to place your test classes in /src/instrumentTest
, and run them by creating an Android test Run Configuration. You can also run the tests from the command line: ./gradlew check
(or, one of the variants, such as connectedCheck
or connectedInstrumentTest
).
Lastly, you should probably mock out all your external dependencies. You might want to look at some mocking framework such as Mockito, or just do stuff yourself.