我有一个Grails集成测试,通过两种测试方法来扩展GroovyTestCase
。第一种方法执行成功,但第二个失败,groovy.lang.MissingMethodException
:为什么一个域类在同一个GroovyTestCase的两个不同方法中表现不同?
失败:testMapBudgetFailure(com.ross.budget.BudgetServiceTests)
groovy.lang.MissingMethodException:方法的无签名:
com.ross .budget.Budget.save()适用于参数类型:()values:[] 可能的解决方案:save(),save(boolean),save(java.util.Map),wait(),last(), any()
at com.ross.budget.BudgetServiceTests.testMapBudgetFailure(BudgetServiceTests.groovy:45)
即使在第一种方法中使用完全相同的方法调用b.save()
。如果我评论第一种方法,第二种测试按预期运行。 为什么这两种测试方法的行为不同?
全类上市:
package com.ross.budget
import grails.test.mixin.*
import org.junit.*
/**
* See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
*/
@TestFor(BudgetService)
class BudgetServiceTests extends GroovyTestCase {
BudgetService budgetService
void testMapBudgetSuccess() {
Budget b = new Budget()
b.month = new Date(2012, 9, 1)
b.amount = new BigDecimal(10.0)
b.save()
Account a = new Account()
a.name = "Test"
a.institution = "Test"
a.description = "Test Account"
a.save()
Transaction t = new Transaction()
t.account = a
t.postDate = new Date(2012, 9, 5)
t.amount = 10.0
t.save()
boolean result = budgetService.mapTransaction(t)
assertTrue("Returned failed match.", result)
assertNotNull("No budget set", t.budget)
}
void testMapBudgetFailure() {
Budget b = new Budget()
b.month = new Date(112, 5, 1)
b.amount = new BigDecimal(10.0)
b.save()
Account a = new Account()
a.name = "Test"
a.institution = "Test"
a.description = "Test Account"
a.save()
Transaction t = new Transaction()
t.account = a
t.postDate = new Date(112, 6, 5)
t.amount = 10.0
t.save()
boolean result = budgetService.mapTransaction(t)
assertFalse("Returned match.", result)
assertNull("Budget set", t.budget)
}
}
我知道代码是复制粘贴,而不是可爱。它的快速测试用例一个个人项目
据我所知,从docs,你应该使用'@ TestFor'或从'GroovyTestCase'扩展,而不是两个 –
非常奇怪,你可以发布域类的代码? –
如果删除'@TestFor(...)'注释,它会起作用吗? –