问题描述
我有一个带有 formatToSpecificText(inputDate: LocalDateTime)
方法的类,可以将日期更改为文本。在这个方法中,我使用了 Duration.between(LocalDateTime.Now(),inputDate)
函数。我的函数返回具有持续时间的指定文本,例如“超过三天”。
我想为这个类和方法编写单元测试。在这种情况下,我想使用 @RunWith(Parametrized::class)
注释。我的测试类看起来像这样:
@RunWith(Parameterized::class)
internal class DateTest(private val case: TestCase) {
private val resources = mock<Resources>()
private val dateFormatter = mock<DateFormatter>()
private lateinit var binder: DateBinder
private val mockedNow: LocalDateTime = LocalDateTime.of(2021,Month.JULY,21,12,0)
@Before
fun setUp() {
binder = DateBinder(resources,dateFormatter)
whenever(
resources.getQuantityString(
R.plurals.over_day,1,1
)
).thenReturn(OVER_1_DAY_LABEL)
}
@Test
fun `should return formatted time string`() {
mockStatic(LocalDateTime::class.java).use { mocked ->
mocked.`when`<Any> { LocalDateTime.Now() }.thenAnswer { mockedNow }
val result = binder.formatToSpecificText(case.given)
assertthat(case.expected).isEqualTo(result)
}
}
private companion object {
const val OVER_1_DAY_LABEL = "over 1 day"
@JvmStatic
@Parameters(name = "{index}: Test with input={0}")
fun testCases(): List<TestCase> = listof(
TestCase(
given = LocalDateTime.of(2021,22,0),expected = OVER_1_DAY_LABEL
)
)
}
internal data class TestCase(
val given: LocalDateTime,val expected: String
)
}
我在 testCases 方法的给定字段中输入的所有内容在 binder.formatToSpecificText() 中都返回 null。
java.lang.NullPointerException
at java.base/java.time.LocalDateTime.until(LocalDateTime.java:1686)
at java.base/java.time.Duration.between(Duration.java:488)
at com.example.DateBinder.formatToSpecificText(DateBinder.kt:17)
我做错了什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)