Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在grails单元测试中获取域实体的结果_Java_Unit Testing_Grails_Groovy - Fatal编程技术网

Java 在grails单元测试中获取域实体的结果

Java 在grails单元测试中获取域实体的结果,java,unit-testing,grails,groovy,Java,Unit Testing,Grails,Groovy,如果查询结果不正确,我的服务将抛出InvalidOperationException PaymentDetails.FindByidAndAmountDateCreatedCreateTaneQuals不为空 该功能旨在检查过去5分钟内是否存在重复的详细信息。 我试图为此创建一个单元测试,但函数总是返回NULL 以下是我的服务方法: Date currentDate = DateUtils.getCurrentDate() Date fiveMinutesBeforeCurren

如果查询结果不正确,我的服务将抛出InvalidOperationException PaymentDetails.FindByidAndAmountDateCreatedCreateTaneQuals不为空

该功能旨在检查过去5分钟内是否存在重复的详细信息。 我试图为此创建一个单元测试,但函数总是返回NULL

以下是我的服务方法:

    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中的东西,我在这方面遇到了困难。请温柔一点。Lol.

问题是新的PaymentDetailsamount:amount,id:methodId实际上无效,因为默认情况下id被排除在mass属性绑定之外,因此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
    }
}

我希望这能有所帮助。

你好,先生!非常感谢你的帮助!我更新了查询,但仍然得到一个空值。几分钟后,我注意到原因是我们的数据库中有一个刻度和精度设置的量-\再次感谢!
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
    }
}