2015-11-27 5 views
6

- 私と活動が、異なるアプリケーションクラスに(例えば、モックまたは継承)。私はandroidTest/AndroidManifest.xml内のアプリケーションタグのreplace = "android:name"というマニフェストマージとツールを使用して、ライブラリプロジェクトのためにこれを取得できました。ただし、これはアプリケーションでは機能しません。テストアンドロイドJUnit4とActivityTestRuleと活性を試験する方法を探して、異なるアプリケーションクラス

どのようにこれを行うことができますか?

答えて

16

独自のランナーとAndroidJUnitRunnerをサブクラス化し、カスタムアプリケーションのクラスでnewApplication()を上書きすることができます。

public class CustomTestRunner extends AndroidJUnitRunner { 

@Override 
public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { 
    return super.newApplication(cl, CustomTestApplication.class.getName(), context); 
} 
} 

このような新しいランナーであなたのbuild.gradleを更新するようにしてください:

testInstrumentationRunner "com.mypackage.name.path.CustomTestRunner"

+2

は、この点を指摘いただきありがとうございます:testInstrumentationRunner "com.mypackage.name.path.CustomTestRunnerを"。多くの他のチュートリアルではこれを実行できませんでした。 –

+2

また、Android Studioからテストを実行している場合、Run/Debug Configurationの 'Specific instrumentation runner'を更新して、CustomTestRunnerを指すようにする必要があります。 – Max

関連する問題