Grails集成测试在test-app中不起作用,但在单独启动时运行正常

时间:2014-03-10 10:39:05

标签: grails mocking spock

我正在测试一个做一点gorm-action的服务类。 如果我单独运行测试作为集成测试它运行没有失败,如果我运行test-app(如果我运行测试应用程序集成无关紧要)我的测试失败并且我收到错误消息,即使用的域类:
“类[xx.xx.User]上的方法在Grails应用程序之外使用。如果在测试的上下文中使用模拟API或正确引导Grails运行。”

由于它是集成测试,我不想模拟域类,我只是不明白这个错误。我使用grails 2.3.5与正确的tomcat和hibernate插件:

@TestFor(EntryService)
//@Mock([User, Task, Entry, Admin])
class EntryServiceSpec extends Specification {

    Entry entry1
    EntryService entryService
    User user
    Task task
    Date date

    def setup() {
        entryService = new EntryService()
        user = User.findByEmail("test@test.de")
        task = Task.findByName("something_inserted")
        date = new Date().clearTime()

        entry1 = new Entry()
        entry1.comment = ""
        entry1.effort = 23 as BigDecimal
        entry1.user = user
        entry1.bookedTask = task
        entry1.bookedCosts = 300 as BigDecimal
        entry1.entryDay = new Date().clearTime()
        entry1.save(flush: true)
    }

    def cleanup() {
        if(entry1 != null && entry1.id != null) {
            entry1.delete()
        }
    }

    void "Wished effort that shall be added is exceeding 24-hours day-constraints"() {
        expect: "user has 23h erfforts, wants to add 2 more hours, it should exceed"
            entryService.isEntryEffortExceedingHoursConstraintsPerDay(user, date, new BigDecimal(2)) == true
    }

    void "Wished effort that shall be added is not exceeding 24-hours day-constraints"() {
        expect: "user has 23h erfforts, wants to add 1 more hours, it should not exceed"
            entryService.isEntryEffortExceedingHoursConstraintsPerDay(user, date, new BigDecimal(1)) == false
    }

    void "null parameter should leed to return false"() {
        expect: "user is null, method should return false"
            entryService.isEntryEffortExceedingHoursConstraintsPerDay(null, date, new BigDecimal(1)) == false

        and: "date is null, method should return false"
            entryService.isEntryEffortExceedingHoursConstraintsPerDay(user, null, new BigDecimal(1)) == false

        and: "wished-effort is null, method should return false"
            entryService.isEntryEffortExceedingHoursConstraintsPerDay(user, date, null) == false
    }
}

1 个答案:

答案 0 :(得分:2)

感谢@dmahapatro,他在上面的评论中回答了这个问题

您正在将测试作为单元测试运行,而不是作为集成测试...

将您的班级签名更改为:

import grails.test.spock.IntegrationSpec

class EntryServiceSpec extends IntegrationSpec

请注意我也删除了@TestFor(EntryService)

相关问题