Getting null pointer object on hilt inject object in activity when run test cases

Viewed 745

Please check below my Module class in which I have defined my object which need to be inject using Hilt

NVModule.kt

     @Module
     @InstallIn(SingletonComponent::class)
     class NVModule {

     @Provides
     @Named("ProfileHelper")
     fun  abprovideProfileHelper(): ProfileHelper {
     return ProfileHelper(AppController.getInstance())
      }
     }

And now please check my Interface by which i have used the EntryPoint to access my dependency injection outside the Activity/Fragment like Helper class.

 @EntryPoint
       @InstallIn(SingletonComponent.class)
       public interface CommonHiltInterface {
       @Named("ProfileHelper")
       public ProfileHelper provideProfileHelper();
       }
      } 

Now please check the my Activity class on which i have used the dependency injection like below and it is working fine here. Means getting dependency injection properly

   public class HomeActivity extends BaseActivity{
    private ActivityHomescreenBinding 
    activityHomescreenBinding;
    private Activity context;
    
   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   context = this;

   activityHomescreenBinding = 
   DataBindingUtil.inflate(getLayoutInflater(), 
   R.layout.activity_homescreen, null, false);
   setContentView(activityHomescreenBinding.getRoot());

   
  CommonHiltInterface commonHiltInterface = EntryPointAccessors.fromApplication(context, CommonHiltInterface.class);

commonHiltInterface.provideProfileHelper().setData();
 
     }
    }
   

But in case of the Test cases , dependency injection getting NullPointerException . I am using the Robolectric for the test cases. Please check my below lines of code for the RobolectricTest case.

   @HiltAndroidTest
   @RunWith(RobolectricTestRunner.class)
   @Config(application = HiltTestApplication.class,
   sdk =  Build.VERSION_CODES.N, manifest = Config.NONE)
   public class HomeActivityTest {

   @Rule
   public HiltAndroidRule hiltRule = new 
   HiltAndroidRule(this);

   @Before
   public void setUp() throws Exception {
   shadowOf(Looper.getMainLooper()).idle();
   hiltRule.inject();
   activity = 
   Robolectric.buildActivity(HomeActivity.class).
   create().resume().get();
    }
   }

Note :- 1). I have also use @HiltAndroidApp() for application class.and using 2.36 version for hilt dependency
2). My dependency injection working fine for the Java classes like Activity/Fagment and Helper classes , But not working in test cases.

Please check my dependency for Hilt are as follow

testImplementation 'com.google.dagger:hilt-android-testing:2.36'
kaptTest 'com.google.dagger:hilt-android-compiler:2.36'
testAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.36'
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.36'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.36'

Application runs successfully but in case of Test case I am getting the null pointer exception below lines of code in Activity (HomeActivity).

 CommonHiltInterface commonHiltInterface = EntryPointAccessors.fromApplication(context, CommonHiltInterface.class);

commonHiltInterface.provideProfileHelper().setData();
0 Answers
Related