I have an intensive addiction application (dagger2). I would like to run an espresso test without checking the test through the entire application, and enter the application.
I would like to start with my teleActivity and make fun of the entry manager. However, in any @test function, we already hit the null pointer, as we called onCreate. If I override this before we run the action (show below), the action will be null.
As far as I understand, the ability to switch our underline dependencies is the big reason we use Dagger2, otherwise it will be just a redesigned singleton. How to override, make fun of or switch the injection into the dagger testing module, so I can create this simple espresso test.
Note. I also wrote all this in the MVP design pattern, if that matters.
TeleActivity
@Inject TelePresenter mTelePresenter; @Inject public LoginStateManager mLoginStateManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ButterKnife.bind(this); DaggerInjectorTele.get().inject(this); mTelePresenter.setTeleDependencies(this); Intent intent = getIntent(); String searchId = null; if (intent != null) { searchId = intent.getStringExtra(Constants.SEARCH_ID); } mTelePresenter.onCreateEvent(searchId, Helper.makeAuthorizationHeader(
Espresso
@LargeTest @RunWith(AndroidJUnit4.class) public class TeleTest { @Rule public ActivityTestRule<TeleActivity> mActivityTestRule = new ActivityTestRule( TeleActivity.class) { @Override protected void beforeActivityLaunched() { super.beforeActivityLaunched(); TeleActivity teleActivity = (TeleActivity)getActivity();
Dagger injector
public class DaggerInjectorTele { private static TelePresenterComponent telePresenterComponent = DaggerTelePresenterComponent.builder().build(); public static TelePresenterComponent get() { return telePresenterComponent; } }
TelePresenterComponent
@Singleton @Component(modules = {TelePresenterModule.class, LoginStateManagerModule.class}) public interface TelePresenterComponent { void inject(TeleActivity activity); }
TelePresenterModule
@Module public class TelePresenterModule { @Provides @Singleton public TelePresenter getTelePresenter() { return new TelePresenter(); } }
LoginStateManagerModule
@Module public class LoginStateManagerModule { @Provides @Singleton public LoginStateManager getLoginStateManager(){ return new LoginStateManager(); } }