I am trying to write test for Dagger_Hilt (hilt-android-testing:2.38.1), but unfortunately this error is occurring when I run the test. I can't understand what's the problem. It would be of much help if anyone could help me to resolve this error.
cannot find symbol import dagger.hilt.android.internal.Contexts; ^
symbol: class Contexts location: package dagger.hilt.android.internal
The ShoppingDaoTest.kt class:
@ExperimentalCoroutinesApi
@SmallTest
@HiltAndroidTest
class ShoppingDaoTest {
@get:Rule
var hileRule = HiltAndroidRule(this)
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Inject
@Named("test_db")
lateinit var database: ShoppingItemDatabase
private lateinit var dao: ShoppingDao
@Before
fun setup() {
hileRule.inject()
dao = database.shoppingDao()
}
@After
fun teardown() {
database.close()
}
@Test
fun insertShoppingItem() = runBlockingTest {
val shoppingItem = ShoppingItem("name", 1, 1f, "url", id = 1)
dao.insertShoppingItem(shoppingItem)
val allShoppingItems = dao.observeAllShoppingItems().getOrAwaitValue()
assertThat(allShoppingItems).contains(shoppingItem)
}
}
The TestModule.kt class :
@Module
@InstallIn(SingletonComponent::class)
object TestAppModule {
@Provides
@Named("test_db")
fun provideinMemoryDb(
@ApplicationContext context: Context
) = Room.inMemoryDatabaseBuilder(context, ShoppingItemDatabase::class.java)
.allowMainThreadQueries()
.build()
}
The HiltTestRunner.kt class :
class HiltTestRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
// return super.newApplication(cl, className, context)
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
and the build.gradle setting :
android {
defaultConfig {
...
testInstrumentationRunner "com.mypackage.name.HiltTestRunner"
}
}
dependencies{
...
// Local Unit Tests
implementation "androidx.test:core:1.4.0"
testImplementation "junit:junit:4.13.2"
testImplementation "org.hamcrest:hamcrest-all:1.3"
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation "org.robolectric:robolectric:4.3.1"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.2.1"
testImplementation "com.google.truth:truth:1.0.1"
testImplementation "org.mockito:mockito-core:2.21.0"
// Instrumented Unit Tests
androidTestImplementation "junit:junit:4.13.2"
androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:2.12.1"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.2.1"
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation "com.google.truth:truth:1.0.1"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation "org.mockito:mockito-core:2.21.0"
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'
}