Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails集成测试没有';无法在测试应用程序中工作,但单独启动时运行正常_Grails_Mocking_Spock - Fatal编程技术网

Grails集成测试没有';无法在测试应用程序中工作,但单独启动时运行正常

Grails集成测试没有';无法在测试应用程序中工作,但单独启动时运行正常,grails,mocking,spock,Grails,Mocking,Spock,我正在测试一个服务类,它执行一些gorm操作。 如果我以集成测试的形式单独运行测试,那么它将在没有故障的情况下运行;如果我运行测试应用程序(无论我是否运行测试应用程序集成:),那么我的测试将失败,并收到错误消息,即所用的域类: “类[xx.xx.User]上的方法在Grails应用程序外部使用。如果在测试上下文中正确使用模拟API或引导Grails运行。” 因为这是一个集成测试,我不想模拟域类,我只是不理解这个错误。我使用的grails 2.3.5带有正确的tomcat和hibernate插件:

我正在测试一个服务类,它执行一些gorm操作。 如果我以集成测试的形式单独运行测试,那么它将在没有故障的情况下运行;如果我运行测试应用程序(无论我是否运行测试应用程序集成:),那么我的测试将失败,并收到错误消息,即所用的域类:
“类[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
    }
}

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

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

将您的类签名更改为:

import grails.test.spock.IntegrationSpec

class EntryServiceSpec extends IntegrationSpec

注意,我还删除了
@TestFor(EntryService)

您的文件位于unser
test/integration
,对吗?请尝试删除TestFor注释是。unter测试/集成集成规范不应具有
@TestFor
,并且应扩展
IntegrationSpec
。新集成测试的自动集成测试创建不是很令人满意。也很难找到相关的文档。谢谢。你的评论很有帮助!