简体   繁体   中英

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. 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:
"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."

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:

@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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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