我正在尝试对将带有复合键的对象插入数据库的方法进行单元测试。每当我为此进行单元测试时,都会得到以下结果。
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
}
域结构是由另一方定义的,我无法以任何方式更改它,因此组合键是必须的。不幸的是,此应用程序中的许多类都使用组合键,因此,如果无法对它们进行单元测试,则单元测试将无法覆盖我的很多代码。任何让我朝着正确方向前进的信息都将非常有用。
最佳答案
不要使用单元测试来测试持久性。
单元测试具有GORM实现,但是它没有数据库支持,只有ConcurrentHashMap
支持。很好,但是仅在避免对 Controller 等其他 Artifact 类型进行单元测试时,才应使用它来避免模拟持久层。如果要测试持久性,请使用数据库。
否则,您将看到类似这样的时髦问题,以及诸如误报或更糟的类似问题-误报使测试不应该通过,从而将错误保留在“经过良好测试”的代码中。
关于unit-testing - 使用复合键的Grails单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29220894/