Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DaggerMock rule in Espresso with injected dependencies used in App's onCreate #88

Open
mariusgherman opened this issue Nov 8, 2018 · 1 comment

Comments

@mariusgherman
Copy link

mariusgherman commented Nov 8, 2018

I want to write an Espresso test and mock dependencies that are also used in Application's onCreate.

I have an app that needs to register lifecycle observers in Application onCreate. The lifecycle observer is an @Inject dependency

public class MyApplication extends Application {
        @Inject
        lateinit var lifecycleObserver: LifecycleObserver

        override fun onCreate() {
            super.onCreate()
            inject()
            ProcessLifecycleOwner.get().getLifecycle().addObserver(lifecycleObserver);
        }
}

What I'm seeing that the DaggerMock rule is created after the App onCreate is called in my Espresso test.

Is there anyway around this, to get my DaggerMock rule created before my Application's onCreate and provide me a mock of lifecycleObserver ?
Thanks

@fabioCollini
Copy link
Owner

Hi, sorry for the late answer :(
I am not sure if you can create before the app is created because usually you need the application object to create the Dagger component.
How do you set the component in the application object? Maybe you can inject the app again and register the observer when you set the component created by DaggerMock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants