I'm building an unit test using grails GORM. When i call list() method to receive data, the list returns empty. Here is controller code: println "entity = "+Entity.get(1)
println "list = "+NotificationProfile.list().size()
params.max = Math.min((params.max as Integer) ?: 10, 100)
User user = User.get(springSecurityService.principal.id)
println "user = "+user
The NotificationProfile.List().size returns 0 The User.get(springSecurityService.principal.id) returns null
Here is the spec code:
@Rollback
@TestFor(NotificationProfileController)
@Mock([NotificationProfile, Entity, User])
class NotificationProfileControllerSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "list by system admin"() {
when:
controller.springSecurityService = [principal: [id: 5]]
controller.list()
then:
view == '/notificationProfile/list'
}
}
I will be happy if i can have a good return in list size and a non null user. Thanks.
When you are doing unit test, Grails doesn't use your database connections and hence, for every unit test there is no data.
In order to populate your database for the life of the unit tests being run, you would add your data in your setup() method.
def setup() {
// make sure you are creating a new user with all the required fields
// otherwise GORM will throw validation error
new User(username: "test", email:"test@test.com").save flush:true, failOnError:true
}