We have a Custom Application which has field injection via an Entry point Accessor. The Field injection works fine in the production code as the Custom Application's super class is Annotated with @HiltAndroidApp
But when the Custom Application is subclassed and used as a Hilt Test Application in Android Test Runner.
The Field injection fails as the component is not yet generated. It errors out saying "The component was not created. Check that you have added the HiltAndroidRule."
The Custom application's onCreate is called before the @Rule applies as the rule is in the Test Class.
Is there way to get the the components generated even before the HiltAndroidRule is applied?
class AppCore: Application{
@Inject B b;
fun onCreate(){
AppInitializerEntryPoint entryPoint = EntryPointAccessors.fromApplication(
getApplicationContext(),
AppInitializerEntryPoint.class);
}
}
@HiltAndroidApp
class HiltAppCore : AppCore()
Test Runner
public final class TestRunner extends AndroidJUnitRunner {
@Override
public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(cl, PpbTestHiltApp_Application.class.getName(), context);
}
Custom Test Application class
@CustomTestApplication(AppCore::class)
interface PpbTestHiltApp
Test Class
@HiltAndroidTest
@RunWith(AndroidJUnit4.class)
@UninstallModules(AppLevelOverridesModule.class)
public class NavigationControllerTest {
public HiltAndroidRule hiltRuleLocal = new HiltAndroidRule(this);
@Rule
public final org.junit.rules.RuleChain mRuleChain;
public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
public androidx.test.rule.ActivityTestRule activity;
public androidx.test.rule.GrantPermissionRule grantPermissionRule = androidx.test.rule.GrantPermissionRule.grant(android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
public NavigationControllerTest() {
super();
mRuleChain = org.junit.rules.RuleChain.outerRule(hiltRuleLocal)
.around(activity)
.around(grantPermissionRule)
.around(screenshotTestWatcher);
}