简体   繁体   English

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

[英]Grails integration test doesn't work in test-app but is running correctly when starting alone

I am testing a service-class which do a little gorm-action. 我正在测试一个做一点gorm-action的服务类。 If i run the test alone as integration-test it runs without failures and if i run test-app (does not matter if i run test-app integration:) my test fails and i got the error message, that the used domain classes: 如果我单独运行测试作为集成测试它运行没有失败,如果我运行test-app(如果我运行测试应用程序集成无关紧要)我的测试失败并且我收到错误消息,即使用的域类:
"Method on class [xx.xx.User] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly." “类[xx.xx.User]上的方法是在Grails应用程序之外使用的。如果在测试的上下文中使用模拟API或正确引导Grails运行。”

Since its an integration-test, i dont want to mock the domain-classes and i just dont understand this error. 由于它是一个集成测试,我不想模拟域类,我只是不明白这个错误。 I am using grails 2.3.5 with the correct tomcat and hibernate plugin: 我使用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
    }
}

Give credit to @dmahapatro who answered this in the comments above 感谢@dmahapatro在上面的评论中回答了这个问题

You are running your test as a Unit test and not as a Integration test... 您将测试作为单元测试运行,而不是作为集成测试...

change you class signature to: 将您的班级签名更改为:

import grails.test.spock.IntegrationSpec

class EntryServiceSpec extends IntegrationSpec

notice that I also removed the @TestFor(EntryService) 注意我也删除了@TestFor(EntryService)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM