无法在测试 Room Dao 时使用 HiltAndroidRule
我想使用 Hilt 进行 Room Dao 测试。 我从这个项目中学到了 https://github.com/philipplackner/ShoppingListTestingYT/tree/TestingWithHilt
我的测试失败并显示:
java.lang.IllegalStateException: Hilt test, com.example.allinone.page2.testAndHilt.data.local.TestItemDaoTest,无法使用 @HiltAndroidApp 应用程序,但找到 com.example.allinone.main.MainApplication。要修复此问题,请将测试配置为使用 HiltTestApplication 或使用 @CustomTestApplication 生成的自定义 Hilt 测试应用程序。 在 dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83) 在 dagger.hilt.android.internal.testing.MarkThatRulesRanRule。(MarkThatRulesRanRule.java:63) 在 dagger.hilt.android.testing.HiltAndroidRule。(HiltAndroidRule.java:36) at com.example.allinone.page2.testAndHilt.data.local.TestItemDaoTest.(TestItemDaoTest.kt:27)
在 AppModule 中:
@Module
@InstallIn(SingletonComponent::class)
object AppModuleTest {
@Provides
@Named("test_db")
fun provideInMemoryDB(@ApplicationContext context: Context) =
Room.inMemoryDatabaseBuilder(context, TestItemDataBase::class.java)
.allowMainThreadQueries()
.build()
}
在 DaoTest 中:
@SmallTest
@HiltAndroidTest
//@RunWith(AndroidJUnit4::class)
class TestItemDaoTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Inject
@Named("test_db")
lateinit var database: TestItemDataBase
lateinit var dao: TestItemDao
@Before
fun setup() {
hiltRule.inject()
dao = database.testItemDao()
}
@After
fun teardown() {
database.close()
}
@Test
fun test() {
assertEquals(1, 1)
}
}
问题出在哪里? 我没有使用MainApplication。 而且我想使用
hiltRule.inject()
所以我不知道如何重写。
I want use Hilt for Room Dao testing.
I learned from this project
https://github.com/philipplackner/ShoppingListTestingYT/tree/TestingWithHilt
My testing failed and show that:
java.lang.IllegalStateException: Hilt test, com.example.allinone.page2.testAndHilt.data.local.TestItemDaoTest, cannot use a @HiltAndroidApp application but found com.example.allinone.main.MainApplication. To fix, configure the test to use HiltTestApplication or a custom Hilt test application generated with @CustomTestApplication.
at dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83)
at dagger.hilt.android.internal.testing.MarkThatRulesRanRule.(MarkThatRulesRanRule.java:63)
at dagger.hilt.android.testing.HiltAndroidRule.(HiltAndroidRule.java:36)
at com.example.allinone.page2.testAndHilt.data.local.TestItemDaoTest.(TestItemDaoTest.kt:27)
in AppModule:
@Module
@InstallIn(SingletonComponent::class)
object AppModuleTest {
@Provides
@Named("test_db")
fun provideInMemoryDB(@ApplicationContext context: Context) =
Room.inMemoryDatabaseBuilder(context, TestItemDataBase::class.java)
.allowMainThreadQueries()
.build()
}
in DaoTest:
@SmallTest
@HiltAndroidTest
//@RunWith(AndroidJUnit4::class)
class TestItemDaoTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Inject
@Named("test_db")
lateinit var database: TestItemDataBase
lateinit var dao: TestItemDao
@Before
fun setup() {
hiltRule.inject()
dao = database.testItemDao()
}
@After
fun teardown() {
database.close()
}
@Test
fun test() {
assertEquals(1, 1)
}
}
Where is the problem?
I didn't use MainApplication.
And I want use
hiltRule.inject()
so I don't know how to rewrite.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个 IDE 错误。
只需使缓存无效并重新启动即可。
It is an IDE bug.
Just invalidae cachs and restart.