我正在学习关于Android的测试,但是在使用Dagger Hilt进行测试时,遇到了以下错误:
> 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
当运行测试并且发生在Android生成的类中时,会导致此错误,但我不知道如何解决它。我已经在互联网上查找,但没有找到有关此错误的任何信息。
以下是我的Dao和测试模块:
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)
}
}