使用FragmentScenario进行单元测试Dagger片段

时间:2019-09-16 16:36:00

标签: java android unit-testing dagger-2 android-fragmentscenario

我正在尝试使用Android Jetpack导航组件和片段测试库来测试片段交互。 我的应用程序使用Java + Dagger2作为DI。

要测试导航,我创建了一个JUnit测试:

    @Test
    public void testNavigationToLoginFragment() {
        // Create a mock NavController
        NavController mockNavController = mock(NavController.class);

        // Create a graphical FragmentScenario for the Intro Fragment
        FragmentScenario<IntroFragment> introFragmentScenario = FragmentScenario.launchInContainer(IntroFragment.class);

        // Set the NavController property on the fragment
        introFragmentScenario.onFragment(fragment ->
                Navigation.setViewNavController(fragment.requireView(), mockNavController)
        );

        // Verify that performing a click prompts the correct Navigation action
        onView(withId(R.id.button_login)).perform(click());
        verify(mockNavController).navigate(R.id.action_intro_fragment_to_login_fragment);
    }

每当我运行测试时,都会出现以下错误:

java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: No injector factory bound for Class<XXX>

如何将片段插入其中? DaggerFragments是否可以与FragmentScenario一起使用?

IntroFragment

public class IntroFragment extends DaggerFragment{
    @Inject
    CreateQuoteRecyclerViewAdapter createQuoteRecyclerViewAdapter;

    @Inject
    public ViewModelProvider.Factory factory;

    @inject 
    public MyViewModel viewModel;
    .....

}

MyViewModel.java

class CreateOrSignInViewModel extends BaseViewModel() {
  @Inject
  public  CreateOrSignInBindingState state;


 ......
}

1 个答案:

答案 0 :(得分:1)

在测试类中声明以下函数:

@Config(application = TestApp::class)
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
class MyFragmentTest {

    private val createQuoteRecyclerViewAdapter: CreateQuoteRecyclerViewAdapter = mock()
    private val viewModel: MyViewModel = mock()

    private fun launchFragment(): FragmentScenario<MyFragment> {
      return launchFragmentInContainer(factory = object : FragmentFactory() {
        override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
          return MyFragment().apply {
            createQuoteRecyclerViewAdapter = this@MyFragmentTest.createQuoteRecyclerViewAdapter
            viewModel = this@MyFragmentTest.viewModel
            // assign other deps here as per your needs
          }
        }
      }, themeResId = R.style.AppTheme)
    }

}

TestApp的声明如下:


    class TestApp : Application()

为了避免出错,可能需要使用该错误,这将发生在您的自定义应用程序类的onCreate()方法中,在该方法中构造了Dagger树。

完成此设置后,您可以开始编写单元测试:

@Test
fun `navigation to login screen is correctly performed`() {
    val navController: NavController = mock()

    val scenario = launchFragment()
    scenario.onFragment {
        Navigation.setViewNavController(it.requireView(), navController)
    }

    onView(withId(R.id.button_login)).perform(click())
    verify(mockNavController).navigate(R.id.action_intro_fragment_to_login_fragment)
}