Reputation: 1509
I am attempting to run my android tests using the AndroidJUnitRunner via the command "gradle connectedDebugAndroidTests" and I noticed that when my tests run, my app's Application object is not being created and "onCreate" is not being called. I am guessing this is expected. However, my tests rely on this code being invoked before the tests can run.
Is there a way to get this to happen?
I tried creating a new manifest in the "androidTest" section of my app that defines the "application" attribute, but this doesn't seem to work either :(
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp">
<application
android:name=".MyTestApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" />
</manifest>
Upvotes: 2
Views: 956
Reputation: 12138
You should create a method annotated with @Before and in that method do below code to start your application class :
@Before
public void prepareApplication() {
MyTestApplication app = (MyTestApplication) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
app.onCreate();
}
Upvotes: 4