【问题标题】:Getting the result of a domain entity in grails unit test在 grails 单元测试中获取域实体的结果
【发布时间】:2020-05-18 19:05:03
【问题描述】:

如果查询结果,我的服务会抛出 InvalidOperationException PaymentDetails.findByIdAndAmountDateCreatedGreaterThanEqualsNOT 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


    【解决方案1】:

    问题在于new PaymentDetails(amount: amount, id:methodId) 不是真正有效的,因为ids 默认情况下从批量属性绑定中排除,因此您的PaymentDetails 实例没有您认为的id(您可以验证如果愿意,可以在调试器中检查对象)。更好的想法是让实体通过save 方法分配一个id,然后稍后检索该值以启动查询。这有效:

    import grails.testing.gorm.DataTest
    import grails.testing.services.ServiceUnitTest
    import groovy.time.TimeCategory
    import spock.lang.Specification
    
    class PaymentServiceSpec extends Specification implements ServiceUnitTest<PaymentService>, DataTest{
    
        @Override
        Class[] getDomainClassesToMock() {
            [PaymentDetails]
        }
    
        void "test payment details validation"() {
            given:
            Date currentDate = new Date()
            GroovySpy(DateUtils, global: true)
            1 * DateUtils.getCurrentDate() >> currentDate
    
            Date twoMinutesBeforeCurrentDate
            use (TimeCategory) {
                twoMinutesBeforeCurrentDate = currentDate - 2.minutes
            }
    
            BigDecimal amount = new BigDecimal("5")
    
            PaymentDetails details = new PaymentDetails(amount: amount).save(flush: true)
    
            when:
            service.validateTransactionDetails(details.id, amount)
    
            then:
            InvalidOperationException exception = thrown()
            ApiError.SAME_PAYMENT_DETAILS_WITH_PREVIOUS_TRANSACTION == exception.apiError
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 您好,先生!非常感谢你的帮助!我更新了查询,仍然得到一个空值。片刻之后(Lol),我注意到原因是 amount 在我们的数据库中设置了比例和精度 -_- 再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    相关资源
    最近更新 更多