I'm trying to create Espresso tests and using a `mockWebServer` the thing is when I try to create my `mockWebServer` it calls the real api call and I want to intercept it and mock the response.

My dagger organisation is : 

My App 

```
open class App : Application(), HasAndroidInjector {

    lateinit var application: Application

    @Inject
    lateinit var androidInjector: DispatchingAndroidInjector<Any>

    override fun androidInjector(): AndroidInjector<Any> = androidInjector

    override fun onCreate() {
        super.onCreate()
        DaggerAppComponent.factory()
            .create(this)
            .inject(this)
        this.application = this
    }
}

```

Then MyAppComponent

```
@Singleton
@Component(
    modules = [
        AndroidInjectionModule::class,
        AppModule::class,
        RetrofitModule::class,
        RoomModule::class,
        AppFeaturesModule::class
    ]
)
interface AppComponent : AndroidInjector<App> {

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance application: App): AppComponent
    }
}

```

Then I've created this TestApp

```
class TestApp : App() {

    override fun androidInjector(): AndroidInjector<Any> = androidInjector

    override fun onCreate() {
        DaggerTestAppComponent.factory()
            .create(this)
            .inject(this)
    }
}

```

And this is my TestAppComponent

```
@Singleton
@Component(
    modules = [
        AndroidInjectionModule::class,
        AppModule::class,
        TestRetrofitModule::class,
        AppFeaturesModule::class,
        RoomModule::class]
)
interface TestAppComponent : AppComponent {
    @Component.Factory
    interface Factory {
        fun create(@BindsInstance application: App): TestAppComponent
    }
}

```

Note: Here I've created a new module, called `TestRetrofitModule` where the BASE\_URL is "<http://localhost:8080>", I don't know if I need something else.

Also I've created the `TestRunner`

```
class TestRunner : AndroidJUnitRunner() {

    override fun newApplication(
        cl: ClassLoader?,
        className: String?,
        context: Context?
    ): Application {
        return super.newApplication(cl, TestApp::class.java.name, context)
    }

}

```

And put it on the `testInstrumentationRunner`

Problem 1
---------

I can not use 

```
@Inject
lateinit var okHttpClient: OkHttpClient

```

because it says that it's not initialised.

Problem 2 (Solved thanks Skizo)
-------------------------------

My mockWebServer is not dispatching the responses even-though is not pointing the real api call, is pointing the one that I've put to the TestRetrofitModule, the thing is that I have to link that mockWebServer and Retrofit.
['https://Stackoverflow.com/questions/61539234', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4329781/']
How about `a dagger module` for your `Test Class` with a `ContributeAndroidInjector` in there and do `Inject` on a `@Before` method.

Your `TestAppComponent`:

```
@Component(modules = [AndroidInjectionModule::class, TestAppModule::class])
interface TestAppComponent {
    fun inject(app: TestApp)

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: TestApp): Builder
        fun build(): TestAppComponent
    }
}

```

`TestAppModule` like:

```
@Module
interface TestAppModule {
    @ContributesAndroidInjector(modules = [Provider::class])
    fun activity(): MainActivity

    @Module
    object Provider {
        @Provides
        @JvmStatic
        fun provideString(): String = "This is test."

    }

    // Your other dependencies here
}

```

And `@Before` method of `Test Class` you must be do:

```
@Before
fun setUp() {
    val instrumentation = InstrumentationRegistry.getInstrumentation()
    val app = instrumentation.targetContext.applicationContext as TestApp

    DaggerTestAppComponent.builder().application(app).build().inject(app)

   // Some things other
}

```

An important thing, you must be have (on `build.gradle` module `app`):

```
kaptAndroidTest "com.google.dagger:dagger-compiler:$version_dagger"
kaptAndroidTest "com.google.dagger:dagger-android-processor:$version"

```

Now, when you launch an `Activity` like `MainActivity`, Dagger will inject `dependencies` from your `TestAppModule` instead of `AppModule` before.

Moreover, If you want to `@Inject` to `Test Class`, you can add:

```
fun inject(testClass: TestClass) // On Your TestAppComponent

```

And then, you can call:

```
DaggerTestAppComponent.builder().application(app).build().inject(this) // This is on your TestClass

```

to Inject some dependencies to your `TestClass`.

Hope this can help you!!
I am presuming that you are trying to inject OkHttpClient:

```
@Inject
lateinit var okHttpClient: OkHttpClient

```

in your TestApp class, and it fails. In order to make it work, you will need to add an inject method in your `TestAppComponent`, to inject the overriden TestApp, so that it becomes: 

```
@Singleton
@Component(
    modules = [
        AndroidInjectionModule::class,
        AppModule::class,
        TestRetrofitModule::class,
        AppFeaturesModule::class,
        RoomModule::class]
)
interface TestAppComponent : AppComponent {
    @Component.Factory
    interface Factory {
        fun create(@BindsInstance application: App): TestAppComponent
    }

    fun inject(testApp: TestApp)
}

```

The reason why this is required, is because Dagger is type based, and uses the type of each class to be injected/provided to determine how to generate the code at compile-time. In your case, when you try to inject the TestApp, dagger will inject its superclass (the App class), because it only know that it has to inject the App class. If you have a look at the AndroidInjector interface (that you use in your AppComponent), you will see that it is declared like:

```
public interface AndroidInjector<T> {
    void inject(T instance)
....
}

```

This means that it will generate a method:

```
fun inject(app App)

```

in the AppComponent. And this is why @Inject works in your App class, but does not work in your TestApp class, unless you explicitly provided it in the TestAppComponent.