我正在尝试运行一个测试:
@HiltAndroidTest
class ActionDaoTest {
@get : Rule
var hiltRule = HiltAndroidRule(this)
@get : Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Inject
@Named("test_db")
lateinit var database: MyDatabase
private lateinit var actionDao: ActionDao
@Before
fun setup() {
hiltRule.inject()
actionDao = database.actionDao()
}
@After
fun teardown(){
database.close()
}
@Test
fun insert_assetTrue() = runTest{
val action = ActionEntity("name","description", LocalDate.now())
actionDao.insert(action)
val actionList= actionDao.selectAll().first()
assertThat(actionList).contains(action)
}
}
我得到一个错误:
java.lang.NoSuchMethodError:找不到虚拟方法(Lkotlin/jvm/functions/Function1;(Lkotlinx/coroutines/internal/ThreadSafeHeapNode;在类Lkotlinx/coroutines/internal/ThreadSafeHeap中;或其超类
我降级了
'org.jetbrains.kotlinx:kotlinx协同程序测试:1.6.4'到'org.jetbrains.kotlinx:kotlinx协同程序测试:1.6.0'现在它起作用了。。。
find
方法在协程核心模块中定义。在我的案例中,我错过了在依赖项中添加该模块。我一把它设置成这样,它就工作了:
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'