簡體   English   中英

Grails集成測試在test-app中不起作用,但在單獨啟動時運行正常

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

我正在測試一個做一點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
    }
}

感謝@dmahapatro在上面的評論中回答了這個問題

您將測試作為單元測試運行,而不是作為集成測試...

將您的班級簽名更改為:

import grails.test.spock.IntegrationSpec

class EntryServiceSpec extends IntegrationSpec

注意我也刪除了@TestFor(EntryService)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM