How do you override a module/dependency in a unit test with Dagger 2.0?

后端 未结 8 2434
一个人的身影
一个人的身影 2020-11-30 19:13

I have a simple Android activity with a single dependency. I inject the dependency into the activity\'s onCreate like this:

Dagger_HelloComponen         


        
8条回答
  •  一整个雨季
    2020-11-30 19:53

    I have solution for Roboletric 3.+.

    I have MainActivity which i want to test without injection on create:

    public class MainActivity extends BaseActivity{
    
      @Inject
      public Configuration configuration;
    
      @Inject
      public AppStateService appStateService;
    
      @Inject
      public LoginService loginService;
    
      @Override
        protected void onCreate(Bundle savedInstanceState) {
          super.processIntent(getIntent()); // this is point where pass info from test
          super.onCreate(savedInstanceState)
        ...
      }
      ...
     }
    

    Next my BaseActivty:

    public class BaseActivity extends AppCompatActivity {
    
      protected Logger mLog;
    
      protected boolean isTestingSession = false; //info about test session
    
    
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          if (!isTestingSession) { // check if it is in test session, if not enable injectig
              AndroidInjection.inject(this);
          }
          super.onCreate(savedInstanceState);
      }
    
      // method for receive intent from child and scaning if has item TESTING with true
      protected void processIntent(Intent intent) {
        if (intent != null && intent.getExtras() != null) {
            isTestingSession = intent.getExtras().getBoolean("TESTING", false);
        }
      }
    

    finally my testclass:

    @Before
    public void setUp() throws Exception {
      ...
      // init mocks...
       loginServiceMock = mock(LoginService.class);
       locServiceMock = mock(LocationClientService.class);
       fakeConfiguration = new ConfigurationUtils(new ConfigurationXmlParser());
       fakeConfiguration.save(FAKE_XML_CONFIGURATION);
       appStateService = new AppStateService(fakeConfiguration, locServiceMock, RuntimeEnvironment.application);
    
       // prepare activity
       Intent intent = new Intent(RuntimeEnvironment.application, MainActivity.class);
       intent.putExtra("TESTING", true);
       ActivityController activityController = Robolectric.buildActivity(MainActivity.class, intent); // place to put bundle with extras
    
        // get the activity instance
        mainActivity = activityController.get();
    
    
        // init fields which should be injected
        mainActivity.appStateService = appStateService;
        mainActivity.loginService = loginServiceMock;
        mainActivity.configuration = fakeConfiguration;
    
    
        // and whoala 
        // now setup your activity after mock injection
        activityController.setup();
    
        // get views etc..
        actionButton = mainActivity.findViewById(R.id.mainButtonAction);
        NavigationView navigationView = mainActivity.findViewById(R.id.nav_view);
    
      ....
      }
    

提交回复
热议问题