I wanted to use Dagger2 with Unit Testing and I have found online 2 projects that have different approaches regarding how you would create a TestComponent. The first project uses the approach in which it keeps the AppComponent in main folder and the TestComponent in test folder and the TestComponent would extend the AppComponent. The second project creates both the AppComponent and the TestComponent in the main folder and then the DaggerTestComponent is used in the test folder when writing tests.
With the first approach, the DaggerTestComponent is created with errors as TestComponent is not recognized for some reason, but with the second approach, the DaggerTestComponent is created successfully and with no errors. My question is if you know what does Dagger recommends because I have read this page related to unit testing: https://dagger.dev/dev-guide/testing and I didn't understand that the TestComponent should be created in a different location (the test folder) than the AppComponent (the main folder). Maybe I am wrong.
This is the first project approach and the structure: first project structure and here is the error shown after a unit test is run and the DaggerTestComponent is generated: first project DaggerTestComponent. The TestComponent code looking something like this:
@Singleton
@Component(
modules = [
(ApplicationModule::class),
(RepositoryModule::class)
]
)
interface TestAppComponent : ApplicationComponent {
fun into(appRepositoryTest: AppRepositoryInjectNamedTest)
fun into(appRepositoryTest: AppRepositoryInjectTest)
fun into(baseTest: BaseTest)
}
This is the second project approach and structure: second project structure and the DaggerTestComponent is generated correctly: second project DaggerTestComponent but this time is not extending the AppComponent. The second project TestComponent code looking something like this:
@Component(
modules = [
DependencyModule::class,
FragmentFactoryModule::class
]
)
@AppScope
interface TestComponent {
@Component.Factory
interface TestFactory {
fun create(@BindsInstance app: Application): TestComponent
}
fun fragmentFactory() : FragmentFactory
}
So to state the question again: where does Dagger2 recommend that the TestComponent class should be created? in main folder or in test folder? And in case you suggest test folder can you also give some examples/links? Thank you.