android - 断言在 JetPack Compose 测试中是否抛出异常
问题描述
问题
Button(onClick = { throw IllegalArgumentException() }) {
Text(text = ACTION_THROW_ERROR)
}
我有一个Button
. 在其中onClick
,我扔了一个IllegalArgumentException
. 如何测试是否抛出异常?
我试过的
- 我试过
@Test
注释的expected
参数
@Test(expected = IllegalArgumentException::class)
fun throwsError_1() {
composeRule.onNodeWithText(ACTION_THROW_ERROR).performClick()
}
- 我试过正常
try-catch
try {
composeRule.onNodeWithText(ACTION_THROW_ERROR).performClick()
assert(false)
} catch (e: IllegalArgumentException) {
assert(true)
}
- 我试过了
Assert.assertThrows
Assert.assertThrows(IllegalArgumentException::class.java) {
composeRule.onNodeWithText(ACTION_THROW_ERROR).performClick()
}
这些都不起作用
在此处查看完整的测试文件
问题
- 我在这里想念什么?
- 为什么测试中没有捕获到异常?
- 在上述情况下如何测试是否抛出异常?
解决方案
推荐阅读
- php - Symfony4 自定义密码编码器
- hibernate - 删除多对多关系中的子行时如何防止父行被删除(休眠)
- postgresql - Gcloud Sql postgres时区问题
- anaconda - 无法从 conda 4.8.5 更新到 5.2
- c#-3.0 - 在 C#3.5 中通过引用返回数组
- c++ - 一个数组中的消息和变量名 C++
- mysql - 在 Sails 中如何使用 MySQL 执行 LIKE 操作?
- c# - 无法在 Asp.net Webforms 中获取 excel 导出的下载选项
- shopify - 在 Shopify 中跳过结帐流程
- c# - 包含 RichTextBox 和图片窗口窗体的打印面板