Android Test cannot execute Fragment instrumented tests with Dagger provided ViewModel, hitting NullPointerException on ViewModelProvider param owner

I have a fragment whose instance of ViewModel is provided by a component, scoped to the navigation graph of Main Activity. The Dagger app component was injected into the single Main Activity and grabbed inside the the fragment as a lazy delegated variable.

private val myViewModel: MyViewModel by lazy {
        ViewModelProvider(findNavController().getBackStackEntry(R.id.nav_graph), Factory(this) { stateHandle ->
            (activity as MainActivity).myComponent.myViewModel()
                .create(stateHandle)}).get(MyViewModel::class.java)
    }

The ViewModelProvider.Factory parameter of ViewModelProvider is using a create method from a Factory interface defined in MyViewModel class; this interface is annotated with @AssistedFactory whereas the savedStateHandle is @AssistInject annotated in the MyViewModel constructor.

class MyViewModel @AssistedInject constructor(
    private val myRepository: MyRepository,
    @Assisted val savedStateHandle: SavedStateHandle
    ) : ViewModel() {

    @AssistedFactory
    interface MyViewModelFactory : ViewModelProvider.Factory {
        fun create(savedStateHandle: SavedStateHandle) : MyViewModel
    }

    //Other ViewModel code 
}

App runs fine with the Dagger app component getting my provided instances of MyViewModel through its lazy delegates, but now when I’m trying to write an instrumented test to test some ui fields on the Fragment itself, I’m getting null point exception. Specifically:

java.lang.NullPointerException: Parameter specified as non-null is null: method androidx.lifecycle.ViewModelProvider.<init>, parameter owner
at androidx.lifecycle.ViewModelProvider.<init>(Unknown Source:2)

This tells me that the owner parameter, findNavController().getBackStackEntry(R.id.nav_graph)
is failing with NPE, but I don’t know how to go about resolving it so my test can compile and execute.

Here is my test code so far, please help where I’m missing here, else I might try removing Dagger and instantiating everything just to get testing going, which sounds backwards in retrospect. I’m not even sure if I’m writing this test scenario correctly, as I’m trying a hodge podge of solutions spread through various help forums and this site for solutions to my compile time issues and failure to build.

@RunWith(AndroidJUnit4::class)
class MyFragmentTest {

    @get:Rule
    var activityRule = ActivityScenarioRule(MainActivity::class.java)

    private lateinit var scenario: FragmentScenario<MyFragment>
    private lateinit var binding: FragmentMyBinding
    private lateinit var mockNavController : NavController
    
    @Test
    fun setUp() {
        mockNavController = mock(NavController::class.java)
        scenario = launchFragmentInContainer<MyFragment> {
            MyFragment().also { fragment ->
                fragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
                    if (viewLifecycleOwner != null) {
                        // The fragment’s view has just been created
                        mockNavController.setGraph(R.navigation.nav_graph)
                        Navigation.setViewNavController(fragment.requireView(), mockNavController)
                    }
                }
            }
        }
    }

Any help or guidance in the right direction would be greatly appreciated as I’ve been stuck for a while here. Thank you.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật