android - 如何使用 joda 时间模拟系统日期
问题描述
我正在为使用当前日期进行一些处理的流程编写 UI 测试。这是我在片段中获取当前日期的方法:
val today = dateFormat.format(Date())
在我的测试中,我想使用一个固定的日期。为此,我决定使用Joda time。这是我在测试中实现它的方式:
@Test
fun testAFlow() {
// setting date to fixed value
DateTimeUtils.setCurrentMillisFixed(1610248674000)
// launching activity
activityRule.launchActivity(Intent())
// remainder flow
...
}
测试运行良好而不会中断,但仍在使用实际日期而不是模拟日期。我怎样才能解决这个问题?
解决方案
您将通过调用currentTimeMillis
from获得固定日期DateTimeUtils
Log.d(BuildConfig.app_tag,"time " + DateTimeUtils.currentTimeMillis()); // output: current time millis
DateTimeUtils.setCurrentMillisFixed(1610248674000L);
Log.d(BuildConfig.app_tag,"time " + DateTimeUtils.currentTimeMillis()); // output: 1610248674000
的行为System.currentTimeMillis()
不会改变
推荐阅读
- python - 如何根据用Python找到的关键字在模板word文档中的特定位置插入图像?
- azure-cosmosdb - 在 Azure Cosmos DB 中为分区键编制索引
- javascript - JavaScript - 如何为每次输入显示界面建议
- angular - syncfysion 文件管理器角度组件中的无限滚动功能
- javascript - 向下模拟鼠标滚轮(scrollTo),只需轻轻一按
- r - 如何在比例函数(ggplot2)中应用逆变换?
- firebase - 是否存在仅包含 Go 的 auth 包的更轻量版本的 firebase admin SDK?
- c# - UI 按钮无法正确移动
- c - 如何使用 010 Editor 比较文件,并与模板变量绑定
- javascript - 在 python 中使用 selenium 执行 javascript console.log 的确切方法