Activity のテストなら ActivityInstrumentationTestCase2
、 Service なら ServiceTestCase
を使うべきなんでしょうけど、Android って事あるごとに Context が必要なので、ただのクラスライブラリのテストでも必要なことがシバシバ。
HogeTest.java
public class HogeTest extends InstrumentationTestCase {
/** ApplicationContext を取得します */
private Context getApplicationContext() {
return this.getInstrumentation().getTargetContext().getApplicationContext();
}
}
参考
- [androidの単体テスト(AndroidTestCase) - Androidのド肝] (http://blog.haw.co.jp/android/?p=471) - クラス図がとても役に立ちました
- android - Accessing application context from TestSuite in Setup() before calling getActivity() - Stack Overflow