我在kotlin SmsHandler
中有一个类,并带有一种调用另一类方法的方法determineFiltersPass
,SmsSendingFilters
:
class SmsHandler(val filterPredicates: SmsSendingFilters) {
fun determineFiltersPass(sms: SmsDto): Boolean = with(sms.filters) {
var pass = true
for (filter in FiltersDto::class.memberProperties)
pass = when (FilterType.valueOf(filter.name.toUpperCase())) {
UNIQUE -> if (filter.get(sms.filters) != null) {
val unique = filter.get(sms.filters) as Boolean
pass && if (unique) filterPredicates.isUnique().test(sms) else true
} else pass && true
RECENT -> if (filter.get(sms.filters) != null) {
pass && filterPredicates.shouldSendByTimePeriodFromLastMessage().test(sms)
} else pass && true
else -> pass && true
}
pass
}
}
class SmsSendingFilters {
fun isUnique(): Predicate<SmsDto> = Predicate {
with(it) {
repo.findAllByMessageIdAndMobileNumAndAppIdAndParamMap(messageId!!, mobileNum!!, appId!!, paramMap!!.toString()).isEmpty()
}
}
fun shouldSendByTimePeriodFromLastMessage(): Predicate<SmsDto> = Predicate {
val calendar = Calendar.getInstance()
calendar.time = Date()
with(it.filters.recent ?: "" to "") {
val size = this.second.toInt()
when (IntervalType.valueOf(this.first.toUpperCase())) {
SECOND -> calendar.add(Calendar.SECOND, -1 * size)
MINUTE -> calendar.add(Calendar.MINUTE, -1 * size)
HOUR -> calendar.add(Calendar.HOUR, -1 * size)
MONTH -> calendar.add(Calendar.MONTH, -1 * size)
YEAR -> calendar.add(Calendar.YEAR, -1 * size)
else -> Unit
}
with(it) {
repo.findAllByMessageIdAndMobileNumAndAppId(messageId!!, mobileNum!!, appId!!)
.none { it.dateSent?.toInstant()?.isAfter(calendar.toInstant()) ?: true }
}
}
}
}
现在,我正在测试SmsHandler
类,并且由于我预测将添加更多的过滤器,因此我想创建一个通用模拟,该模拟是从SmsSendingFilters
类调用任何方法时,将返回true
或false
作为我请求。有没有一种方法,而无需明确描述每个方法名称的调用(我猜是通过反射)?
我不是在寻找排序解决方案:
when(filterPredicates.shouldSendByTimePeriodFromLastMessage().test(any()).thenReturn(true)
但是,与此逻辑相符的东西:
when(filterPredicates.<anyMethodInvoked>().test(any())).thenReturn(true)
您可以注册Answer
以更改默认返回值。
例如(使用出色的Mockito-Kotlin包装器):
interface Thingy {
fun foo() : Boolean
fun bar() : Boolean
}
class ThingyTest {
@Test
fun test() {
val t1 = mock<Thingy>(defaultAnswer = Answer { false })
println(t1.foo()) // "false"
println(t1.bar()) // "false"
val t2 = mock<Thingy>(defaultAnswer = Answer { true })
println(t2.foo()) // "true"
println(t2.bar()) // "true"
}
}
当然,在更复杂的情况下,您可能有返回不同类型的方法,在这种情况下,您需要在Answer
实现中更聪明地做一些更聪明的事情!