JUnit テストの Android コンテキストにアクセスする必要があります。
MockContext を使用して AndroidTestCase を拡張しようとしましたが、エラーが発生するたびに (スタブ!)
JUnit テストの Android コンテキストにアクセスする必要があります。
MockContext を使用して AndroidTestCase を拡張しようとしましたが、エラーが発生するたびに (スタブ!)
JUnit テストの代わりに AndroidTestCase を使用するのはどうですか? AndroidTestCase は、必要な場所で使用できる getContext() を使用して Context を提供します。
テストがインストルメンテーション テスト (エミュレーターまたはデバイスで実行) である場合は、単純に使用できます。
Context appContext = InstrumentationRegistry.getTargetContext();
依存関係は次のとおりです。
androidTestCompile 'com.android.support.test:runner:0.5'
テスト クラスが ActivityInstrumentationTestCase2 を拡張する場合は、これを試してください。
InputStream is = null;
try {
is = getInstrumentation().getContext().getAssets().open("your.file");
} catch (IOException e) {
Log.d("Error", "Error during file opening!!!");
}
各アクティビティはコンテキストのサブクラスであるため、コンテキストが必要な場合はアクティビティを使用する必要があります。クラス Context は、アプリケーションからインスタンス化するものではありません。