LoginSignup
33
33

More than 5 years have passed since last update.

Android の jUnit テストで Context が欲しい時

Last updated at Posted at 2013-01-10

Activity のテストなら ActivityInstrumentationTestCase2 、 Service なら ServiceTestCase を使うべきなんでしょうけど、Android って事あるごとに Context が必要なので、ただのクラスライブラリのテストでも必要なことがシバシバ。

HogeTest.java
public class HogeTest extends InstrumentationTestCase {
    /** ApplicationContext を取得します */
    private Context getApplicationContext() {
        return this.getInstrumentation().getTargetContext().getApplicationContext();
    }
}

参考

33
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
33