Why is this Spock Specification in Grails resulting in 'identifier of an instance of projectname.Event was altered from 1 to 2;'?

StackOverflow https://stackoverflow.com/questions/4768172

Question

Running test-app integration:spock causes the following error:

identifier of an instance of projectname.Event was altered from 1 to 2; nested exception is org.hibernate.HibernateException: identifier of an instance of projectname.Event was altered from 1 to 2

org.springframework.orm.hibernate3.HibernateSystemException: identifier of an instance of projectname.Event was altered from 1 to 2;

nested exception is org.hibernate.HibernateException: identifier of an instance of projectname.Event was altered from 1 to 2 at projectname.EventControllerSpec.save: an event(EventControllerSpec.groovy:74)

Caused by: org.hibernate.HibernateException: identifier of an instance of projectname.Event was altered from 1 to 2

  1. Where does this issue stem from?
  2. How can it be resolved?
  3. [OPTIONAL] May somebody create the Tag "spock"?

from EventControllerSpec.groovy

def "save: an event"() {
    given: "Constraint-conform event properties"
    def eventTitle              = "Being in Beijing"
    controller.params.title     = eventTitle
    controller.params.details   = "Details"
    controller.params.location  = "Beijing"
    controller.params.startDate = "01.09.2030"
    controller.params.startTime = "20:15"
    controller.params.endDate   = "01.09.2030"
    controller.params.endTime   = "21:45"
    controller.params.publisher = getUserObject("someuser")

    when: "I save that event"
    def result = controller.save() // THIS IS LINE #74 AS STATED IN THE ERROR

    then: "The event is successfully saved and the show-view rendered"
    controller.flash.message.args.grep(eventTitle)
    redirectArgs.action             == "show"
    redirectArgs.id                 == result.eventInstance.id
}

private User getUserObject(String name) {
    def user = User.findByUsername(name)

    if (!user) {
        user = new User()
        user.username = name
        user.email = "${name}@example.com"
        user.pw = "barbar"
        user.pwConfirmation = "barbar"
        assert user.save()
    }

    user
}

from EventController.groovy

def save = {
    def eventInstance = new Event()

    eventInstance.title     = params.title
    eventInstance.details   = params.details
    eventInstance.location  = params.location
    eventInstance.startDate = DateUtil.createDate(params.startDate, params.startTime)
    eventInstance.endDate   = DateUtil.createDate(params.endDate, params.endTime)
    eventInstance.publisher = session.user

    if (eventInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), eventInstance.title])}"
        redirect(action: "show", id: eventInstance.id)
    }
    else {
        eventInstance.errors.each { log.warn it }
        render(view: "add", model: [eventInstance: eventInstance])
    }
}
Was it helpful?

Solution

What class does your Spock test extend? It should be spock.lang.Specification or grails.plugin.spock.IntegrationSpec rather than grails.plugin.spock.ControllerSpec, which is designed for unit tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top