我有一个Grails集成测试,它使用两种测试方法扩展GroovyTestCase
。第一种方法成功执行,但第二种方法失败并带有groovy.lang.MissingMethodException
:
失败:testMapBudgetFailure(com.ross.budget.BudgetServiceTests)
groovy.lang.MissingMethodException:没有方法签名:
com.ross.budget.Budget.save()适用于参数类型:()值:[] 可能的解决方案:save(),save(boolean),save(java.util.Map),wait(),last(),any()
在 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)
}
}
我知道代码是复制粘贴而不可爱。这是个人项目的快速测试案例