skip to Main Content

I’m studying Tests on Android, but when doing the tests with Dagger Hilt, I’m having the following error:

> Task :app:kaptDebugAndroidTestKotlin
C:UsersHenriqueDevProjetos-AndroidTestingOnAndroidTutorialappbuildgeneratedsourcekaptdebugAndroidTestcomyoutubetutorialstestingonandroidtutorialdiTestAppModule_ProvideInMemoryDbFactory.java:11: error: cannot find symbol
@ScopeMetadata
 ^
  symbol: class ScopeMetadata
C:UsersHenriqueDevProjetos-AndroidTestingOnAndroidTutorialappbuildgeneratedsourcekaptdebugAndroidTestcomyoutubetutorialstestingonandroidtutorialdiTestAppModule_ProvideInMemoryDbFactory.java:12: error: cannot find symbol
@QualifierMetadata
 ^
  symbol: class QualifierMetadata
C:UsersHenriqueDevProjetos-AndroidTestingOnAndroidTutorialappbuildgeneratedsourcekaptdebugAndroidTestcomyoutubetutorialstestingonandroidtutorialdatalocalShoppingDaoTest_MembersInjector.java:10: error: cannot find symbol
@QualifierMetadata
 ^
  symbol: class QualifierMetadata

This error is caused when running the tests and it happens in the classes that are generated by Android, but I don’t know how to solve it, I’ve looked on the internet and haven’t found anything about this error

Below is my Dao and my test module

import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import com.youtube.tutorials.testingonandroidtutorial.data.local.ShoppingItemDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn

@Module
//    @InstallIn(SingletonComponent::class)
@TestInstallIn(components = [SingletonComponent::class], replaces = [AppModule::class])
object TestAppModule {

    @Provides
    fun provideInMemoryDb() = Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), ShoppingItemDatabase::class.java)
        .allowMainThreadQueries().build()
}
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.filters.SmallTest
import com.google.common.truth.Truth.assertThat
import com.youtube.tutorials.testingonandroidtutorial.getOrAwaitValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject

//@UninstallModules(AppModule::class)
@HiltAndroidTest
@ExperimentalCoroutinesApi
@SmallTest
class ShoppingDaoTest {

    @get:Rule
    var hiltRule = HiltAndroidRule(this)

    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    @Inject
    lateinit var database: ShoppingItemDatabase
    private lateinit var dao: ShoppingDao


    @Before
    fun setup() {
        hiltRule.inject()
//        database = Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), ShoppingItemDatabase::class.java)
//            .allowMainThreadQueries().build()
        dao = database.shoppingDao()
    }

    @After
    fun tearDown() {
        database.close()
    }

    @Test
    fun insertShoppingItem(): TestResult = runTest {
        val shoppingItem = ShoppingItem(1, "name", 1, 1f, "url")
        dao.insertShoppingItem(shoppingItem)

        val allShoppingItems: List<ShoppingItem> = dao.getAllShoppingItems().getOrAwaitValue()

        assertThat(allShoppingItems).contains(shoppingItem)
    }

    @Test
    fun deleteShoppingItem() : TestResult= runTest {
        val shoppingItem = ShoppingItem(1, "name", 1, 1f, "url")
        dao.insertShoppingItem(shoppingItem)
        dao.deleteShoppingItem(shoppingItem)

        val allShoppingItems: List<ShoppingItem> = dao.getAllShoppingItems().getOrAwaitValue()

        assertThat(allShoppingItems).doesNotContain(shoppingItem)
    }

    @Test
    fun totalPriceSum(): TestResult = runTest {
        val shoppingItem1 = ShoppingItem(1, "name", 2, 10f, "url")
        val shoppingItem2 = ShoppingItem(2, "name", 4, 5.5f, "url")
        val shoppingItem3 = ShoppingItem(3, "name", 0, 100f, "url")
        dao.insertShoppingItem(shoppingItem1)
        dao.insertShoppingItem(shoppingItem2)
        dao.insertShoppingItem(shoppingItem3)

        val totalPriceSum: Float = dao.getTotalPrice().getOrAwaitValue()

        assertThat(totalPriceSum).isEqualTo(2 * 10f + 4 * 5.5f)
    }
}

2

Answers


  1. Today I also encountered this error. I had implemented dagger-hilt in my project. But the @ScopeMetaData and @QualifierMetaData are based on dagger.
    I solved the problem by adding dagger dependencies in addition to dagger-hilt:

    implementation 'com.google.dagger:dagger:2.41 '
    kapt 'com.google.dagger:dagger-compiler:2.41'
    
    Login or Signup to reply.
  2. My issue was due to having multiple dependencies of the different versions and different dependency notations and I was missing the support lib. For Android using Kotlin I had:

    annotationProcessor 'com.google.dagger:dagger-compiler:2.41'
    kapt 'com.google.dagger:dagger-compiler:2.41'
    
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.24'
    kapt 'com.google.dagger:dagger-android-processor:2.24
    

    Should be:

    implementation 'com.google.dagger:dagger-android-support:2.41'
    kapt 'com.google.dagger:dagger-android-processor:2.41'
    kapt 'com.google.dagger:dagger-compiler:2.41'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search