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/']
The setup you posted looks correct. As for `App` not being provided, you probably need to bind it in your component, since right now you're binding `TestApp` only. So you need to replace

```kotlin
fun create(@BindsInstance application: TestApp): TestAppComponent

```

with

```kotlin
fun create(@BindsInstance application: App): TestAppComponent

```
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!!