使用复合键进行Grails单元测试

时间:2015-03-23 21:37:59

标签: unit-testing grails gorm composite-key

我正在尝试对具有复合键的对象插入数据库的方法进行单元测试。每当我为此进行单元测试时,我都会得到以下结果。

Failure:  |
testTransaction(com.myapp.foo.TestServiceSpec)
 |
Condition not satisfied:

Transaction.count() == 1
            |       |
            0       false

    at com.myapp.foo.TestServiceSpec.testTransaction(TestServiceSpec.groovy:166)

如果我从我的域类中删除复合键码而没有其他内容,则测试通过。

这是我的域类,Transaction.groovy:

class Transaction implements Serializable {
    String timestamp
    String source
    String tableName
    String fieldName
    Integer changeNumber
    String fieldValue
    String objectId

    static mapping = {
        id composite: ["timestamp", "source", "tableName", "fieldName", "changeNumber"], generator: 'assigned'
    }

    boolean equals(other) {
        if (!(other instanceof Transaction)) {
            return false
        }

        other.timestamp == timestamp && other.source == source && other.id == id && other.tableName == tableName && other.fieldName == fieldName && other.changeNumber == changeNumber
    }

    int hashCode() {
        def builder = new HashCodeBuilder()
        builder.append timestamp
        builder.append source
        builder.append tableName
        builder.append fieldName
        builder.append changeNumber
        builder.toHashCode()
    }
}

这是正在测试的代码:

def response = [code: 'OK']
def transaction = new Transaction()

transaction.timestamp  = (new Date()).format("yyyy-MM-dd HH:mm:ss.SSS")
transaction.source       = "APP"
transaction.tableName    = "TABLE_NAME"
transaction.fieldName    = "FIELD_NAME"
transaction.fieldValue   = "FIELD_VALUE"
transaction.objectId     = "OBJECT_ID"

def changeNumber = Transaction.createCriteria().get {
    eq("objid", currentTransaction.objid)
    eq("tableName", currentTransaction.tableName)
    projections {
        max("changeNumber")
    }
}

currentTransaction.changeNumber = (changeNumber ?: 0) + 1

if(!transaction.save()) {
    transaction.errors.each {
        println it
    }

    response = [code: 'error transaction', status: 500]
}

return response

最后,这是我的单元测试代码:

void testTransaction() {
    when:
    def response = testService.loadTransaction() // Creates transaction row

    then:
    assert response == [code: 'OK']
    assert Transaction.count() == 1
}

域结构由另一方定义,我无法以任何方式更改它,因此复合键是必须的。不幸的是,这个应用程序中的许多类都使用复合键,所以如果我不能对它们进行单元测试,那么我的很多代码都不能被单元测试所覆盖。让我走向正确方向的任何信息都会很棒。

1 个答案:

答案 0 :(得分:2)

不要使用单元测试来测试持久性。

单元测试具有GORM实现,但它不受数据库支持,只有ConcurrentHashMap。它非常好,但它应该只用于避免在单元测试其他工件类型(如控制器)时必须模拟持久层。如果要测试持久性,请使用数据库。

否则,您会看到类似这样的时髦问题,以及类似的问题,例如错误否定或更糟糕的错误 - 在测试通过时不会出现错误,会在您的“经过良好测试”的代码中留下错误。