我有一个Grails集成测试,它使用两种测试方法扩展了 GroovyTestCase
。第一种方法执行成功,但是第二种方法失败,并带有groovy.lang.MissingMethodException
:
Failure: testMapBudgetFailure(com.ross.budget.BudgetServiceTests)
groovy.lang.MissingMethodException: No signature of method:
com.ross.budget.Budget.save() is applicable for argument types: () values: [] Possible solutions: 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)
}
}
我知道代码是复制粘贴,并不可爱。这是个人项目的快速测试案例
最佳答案
根据Grails doc,您应该使用@TestFor
进行单元测试,或者扩展GroovyTestCase
进行集成测试,而不要同时使用两者。
关于grails - 为什么域类在同一个GroovyTestCase的两种不同方法中表现不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13335755/