Spock UnitTest,调用太少,但在 "不匹配调用 "列表中的调用完全相同。

问题描述 投票:0回答:1

我正在为我的一些计算时间的代码写一个单元测试,我使用SQL.date作为输入,并将其转换为日历进行计算。(是的,我知道Joda会更好)。

我的测试如下。

def objectOrgUnit = Stub(ConOrgUnit)

def notification

def setup(){
    objectOrgUnit = Stub(ConOrgUnit) {
        getLogicalName() >> "Multimedia"
    }
}

def "CreateNotification creates Notification with correctly caluclated dates"() {
    given:
    def date = new java.sql.Date(2020-1900,4,1)

    def not = new NotificationGenerator()
    def contract = GroovyMock(GenericBean) {
        getCtrInsuranceDateToDat() >> date
    }
    def vocs = GroovyStub(ControlledVocabularyAccess) {
        getNodesByInstanceAndName('TasStatusVgr', 'open') >> [Stub(ControlledVocabularyNode)]
        getNodesByInstanceAndName('TasTypeVgr', 'TYPE') >> [Stub(ControlledVocabularyNode)]
    }

    def newNotification = GroovyMock(GenericBean) {
        getTasContract02Ref() >> GroovyMock(GenericBean)
        getTasSendEmailGrp() >> GroovyMock(GenericBean)
    }

    def dataAccess = GroovyStub(DataAccess) {
        createObject("Task") >> newNotification
    }
    def orgUnitAdminService = Stub(OrgUnitAdminServiceAccess) {
        readUsingLogicalName("org") >> objectOrgUnit
    }
    not.metaClass.vocs = vocs
    not.metaClass.dataAccess = dataAccess
    not.metaClass.orgUnitAdminService = orgUnitAdminService
    when:
    notification = not.createNotification(contract, 2, "REASON", "TYPE", "[email protected]", "org")
    then:
    1 * newNotification.setTasDeadlineDat(2020-02-01)
    1 * newNotification.setTasDeadlineToDat(2020-02-01)
}

当运行时,它给我的错误。

1 * newNotification.setTasDeadlineDat(2020-02-01)   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * newNotification.setTasDeadlineDat(2020-02-01)

所以它看起来会是正确的吗?我也试着测试 "date",但也没有用。

代码本身是这样的(部分)。

    Calendar c = Calendar.getInstance()
    c.setTime(contract.CtrInsuranceDateToDat)
    c.add(Calendar.MONTH, -1 * (months + 1))
    taskNotification.TasDeadlineDat = new java.sql.Date(c.getTime().getTime())
    taskNotification.TasDeadlineToDat = new java.sql.Date(c.getTime().getTime())
groovy spock
1个回答
0
投票

好吧,我不能运行你的测试,但我想我发现了它。

then:
1 * newNotification.setTasDeadlineDat(2020-02-01)
1 * newNotification.setTasDeadlineToDat(2020-02-01)

2020-02-01 只是一个减法 2020 - 2 - 12017而不是日期,既不是简单的Java日期,也不是SQL日期(SQL日期是Java日期的一个子类,所以要小心)。

我想你更想要的是这样的东西。

given:
def expectedDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-02-01")

// ...

then:
1 * newNotification.setTasDeadlineDat(expectedDate)
1 * newNotification.setTasDeadlineToDat(expectedDate)

现在你真的是在比较日期了 它适用于比较 java.sql.Datejava.util.Date 因为后者并不优先于 equals(..) 前者的方法。

© www.soinside.com 2019 - 2024. All rights reserved.