Rui Santiago Rui Santiago - 1 year ago 139
Groovy Question

Grails Spock GORM returns an empty list

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(

println "user = "+user

The NotificationProfile.List().size returns 0
The User.get( returns null

Here is the spec code:

@Mock([NotificationProfile, Entity, User])
class NotificationProfileControllerSpec extends Specification {

def setup() {

def cleanup() {

void "list by system admin"() {

controller.springSecurityService = [principal: [id: 5]]

view == '/notificationProfile/list'


I will be happy if i can have a good return in list size and a non null user. Thanks.

Answer Source

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:"").save()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download