0

我有一个GroovyTestCase使用两种测试方法扩展的 Grails 集成测试。第一种方法成功执行,但第二种方法失败并显示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()
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)

    }
}

我知道代码是复制粘贴而不是可爱。这是个人项目的快速测试用例

4

1 回答 1

1

根据Grails doc,您应该将其@TestFor用于单元测试或扩展GroovyTestCase用于集成测试,而不是两者兼而有之。

于 2012-11-13T13:13:34.223 回答