Reputation: 553
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("[email protected]")
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
}
}
Upvotes: 3
Views: 2110
Reputation: 2764
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)
Upvotes: 2