【发布时间】:2020-05-18 19:05:03
【问题描述】:
如果查询结果,我的服务会抛出 InvalidOperationException
PaymentDetails.findByIdAndAmountDateCreatedGreaterThanEquals 是 NOT NULL
该功能旨在检查过去 5 分钟内是否有重复的详细信息。
我正在尝试为此创建一个单元测试,但该函数始终返回NULL
given:
DateUtils.getCurrentDate() >> new Date()
Date currentDate = new Date()
Date twoMinutesBeforeCurrentDate = new Date()
use (TimeCategory) {
twoMinutesBeforeCurrentDate = currentDate - 2.minutes
}
long methodId = 3L
BigDecimal amount = new BigDecimal("5")
PaymentDetails details = new PaymentDetails(amount: amount, id:methodId).save(flush: true)
details.dateCreated = twoMinutesBeforeCurrentDate
details.save(flush: true)
when:
service.validateTransactionDetails(methodId, amount)
then:
InvalidOperationException exception = thrown()
ApiError.SAME_PAYMENT_DETAILS_WITH_PREVIOUS_TRANSACTION == exception.apiError
这是我的服务方法:
Date currentDate = DateUtils.getCurrentDate()
Date fiveMinutesBeforeCurrentDate = null
use (TimeCategory) {
fiveMinutesBeforeCurrentDate = currentDate-5.minutes
}
PaymentDetails details = PaymentDetails.findByIdAndAmountDateCreatedGreaterThanEquals(methodId, amount, fiveMinutesBeforeCurrentDate)
if (details) {
throw new InvalidOperationException(ApiError.SAME_PAYMENT_DETAILS_WITH_PREVIOUS_TRANSACTION)
}
提前感谢您!这是我第一次从 Grails 调试一些东西,我很难做到这一点。请温柔一点。哈哈。
【问题讨论】:
标签: java unit-testing grails groovy