Nikola Milovic
11/18/2019, 9:05 AM@Module(includes = [TestApplicationModuleBinds::class])
object TestApplicationModule {
@set:Inject
lateinit var mockQuoteDataSource : MockQuoteDataSource
class MockQuoteDataSource @Inject constructor(jsonNetworkService: JsonNetworkService) : TestQuoteRemoteDataSource(jsonNetworkService.apiService) {
I get the error
kotlin.UninitializedPropertyAccessException: lateinit property mockQuoteDataSource has not been initialized
tseisel
11/18/2019, 9:29 AMNikola Milovic
11/18/2019, 9:32 AM@Test
fun willDisplayQuote() {
TestApplicationModule.mockQuoteDataSource.mockSuccess(listOf(Quote("Expected Author", "Expected Quote")))
I'm trying to use the mockQuoteDataSource in my tests, should i move it all together somewhere else?tseisel
11/18/2019, 10:22 AMContext
or SharedPreferences
, and you should avoid instance fields in modules (especially if those are object
). I think you need a Component
.
Depending on the type of test you are writing, you should proceed the following :
- Unit Tests : just don't use Dagger. Instantiate the class under test yourself, passing its dependencies as mocks or fakes.
- Integration Tests : create a Dagger Component
responsible for instantiating the class under test with its real dependencies. This component should reference alternative version of the production `Module`s, for example simulating a network API or using an in-memory database. More information here : https://dagger.dev/testingNikola Milovic
11/18/2019, 10:23 AM