【问题标题】:Why does a domain class behave differently in two different methods of the same GroovyTestCase?为什么域类在同一个 GroovyTestCase 的两个不同方法中表现不同?
【发布时间】:2012-11-11 21:41:53
【问题描述】:

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

    }
}

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

【问题讨论】:

  • 据我从文档中了解到,您应该使用@TestFor 或从GroovyTestCase 扩展,而不是两者都使用
  • 很奇怪,能贴出领域类的代码吗?
  • 去掉@TestFor(...)注解还能用吗?
  • @denis.solonenko 成功了。你能把它作为答案发布吗,我会接受的。

标签: grails groovy integration-testing


【解决方案1】:

根据Grails doc,您应该使用@TestFor 进行单元测试或扩展GroovyTestCase 进行集成测试,不能同时使用两者。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    相关资源
    最近更新 更多