Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger Hilt Testing Error - error: cannot find symbol @ScopeMetadata, @QualifierMetadata

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

> Task :app:kaptDebugAndroidTestKotlin
C:\Users\Henrique\Dev\Projetos-Android\TestingOnAndroidTutorial\app\build\generated\source\kapt\debugAndroidTest\com\youtube\tutorials\testingonandroidtutorial\di\TestAppModule_ProvideInMemoryDbFactory.java:11: error: cannot find symbol
@ScopeMetadata
 ^
  symbol: class ScopeMetadata
C:\Users\Henrique\Dev\Projetos-Android\TestingOnAndroidTutorial\app\build\generated\source\kapt\debugAndroidTest\com\youtube\tutorials\testingonandroidtutorial\di\TestAppModule_ProvideInMemoryDbFactory.java:12: error: cannot find symbol
@QualifierMetadata
 ^
  symbol: class QualifierMetadata
C:\Users\Henrique\Dev\Projetos-Android\TestingOnAndroidTutorial\app\build\generated\source\kapt\debugAndroidTest\com\youtube\tutorials\testingonandroidtutorial\data\local\ShoppingDaoTest_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)
    }
}
like image 650
HENRIQUE SANTOS DA SILVA Avatar asked Sep 12 '25 23:09

HENRIQUE SANTOS DA SILVA


1 Answers

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'
like image 126
Fateh Zaliyev Avatar answered Sep 14 '25 15:09

Fateh Zaliyev