如何在Grails测试中访问内部枚举?

时间:2015-10-29 12:30:01

标签: unit-testing grails groovy enums spock

我有一个像这样的Groovy域:

package fantasy

    class Notification {

        enum NotificationType {
            INVITE_GENERAL, INVITE_CONTEST
        }

        NotificationType type

        String emailTo
        User toUser
        User fromUser
        String message
        boolean wasReadByToUser
        boolean wasReadByFromUser
        boolean fromUserRemoved

        Date dateCreated

        static constraints = {
            type(blank: false)
            toUser(nullable: true)
            emailTo(email: true, blank: false)
        }

        static mapping = {
            autoTimestamp true
        }
    }

我需要在Grails.Groovy / Spock测试中访问通知类型......

我怎样才能进入Enum,因为我已经尝试过我能做的一切。这是我到目前为止的测试:

package fantasy

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Notification)
@Mock([Notification, User])
class NotificationSpec extends Specification {


    // Notification
    Notification notification 

    // Fields
    String emailTo = "fmulder@fox.com"
    String message = "Trust No One"
    boolean wasReadByUser = false
    boolean wasReadByFromUser = false
    boolean fromUserRemoved = false

    // Date
    Date dateCreated

    // Mocks
    def toUser
    def fromUser

    def setup() {
        toUser = Mock(User)
        fromUser = Mock(User)
        dateCreated = new Date()

    }

    def cleanup() {
        toUser = null
        fromUser = null
        dateCreated = null
    }

    void "test valid entry will validate"() {
        setup:
        notification = new Notification(
            /* Here is where the problem is, see stacktrace below */
            type: Notification.NotificationType.INVITE_GENERAL,
            emailTo: "danaScully@fox.com",
            toUser: toUser,
            fromUser: fromUser,
            message: message,
            wasReadByUser: false,
            wasReadByFromUser: false,
            fromUserRemoved: false,
            dateCreated: dateCreated
        ).save(failOnError: false, flush: true)
        expect:
        Notification.count() == 1
        notification.validate()
    }

}

如何访问内部枚举?

这是我得到的stackktrace:

显示java.lang.NullPointerException     在org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:993)     在org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:1138)     在org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:160)     在org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:522)     在org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:194)     在org.grails.datastore.gorm.GormInstanceApi.save_closure5(GormInstanceApi.groovy:162)     在org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302)     在org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:37)     在org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:161)     在fantasy.NotificationSpec.test有效条目将验证(NotificationSpec.groovy:73)

0 个答案:

没有答案