grails - 为什么域类在同一个GroovyTestCase的两种不同方法中表现不同?

标签 grails groovy integration-testing

我有一个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/

相关文章:

Groovy:this.metaClass 与 instance.metaClass

sorting - 如何在 Groovy 中对文本+日期字符串列表进行排序

grails - Grails:如何在消息的args中传递链接

grails - Grails用于插入Nexmo插件的错误

mysql - 如何将 mysql 转储加载到 hsqldb 数据库?

ios - 如何使用 Subliminal 模拟 TableView 或 Collection View 滚动?

java - 使用 gradle 将 jar 包含到集成测试中

java - org.apache.cxf.ws.policy.PolicyException : None of the policy alternatives can be satisfied

grails - Grails 3 中的运行脚本替代方案

hibernate - Grails-Hibernate XML-自定义列名