grails 2.4.3 spock测试,接收java.lang.ClassCastException

grails 2.4.3 spock测试,接收java.lang.ClassCastException,grails,groovy,spock,Grails,Groovy,Spock,以下是我们收到的全部错误消息: org.spockframework的版本:spock-core:0.7-groovy-2.0这是Grails2.4.3附带的spock |Running 2 unit tests... |Running 2 unit tests... 1 of 2 Failure: | state constraints check(com.company.srm.state.StateSpec) | java.lang.ClassCastException: java.l

以下是我们收到的全部错误消息:

org.spockframework的版本:spock-core:0.7-groovy-2.0这是Grails2.4.3附带的spock

|Running 2 unit tests...
|Running 2 unit tests... 1 of 2 Failure:  
| state constraints check(com.company.srm.state.StateSpec)
| java.lang.ClassCastException: java.lang.Class cannot be cast to
    com.company.srm.state.State at com.company.srm.state.StateSpec.state
    constraints check(StateSpec.groovy:66)
|Running 2 unit tests... 2 of 2 Failure:
| state name unique constraints check(com.company.srm.state.StateSpec)
| java.lang.ClassCastException: java.lang.Class cannot be cast to
    com.company.srm.state.State at com.company.srm.state.StateSpec.state
    name unique constraints check(StateSpec.groovy:86)
|Running 2 unit tests... 3 of 3 Failure:
| state abbreviation unique constraints check(com.company.srm.state.StateSpec)
| java.lang.ClassCastException: java.lang.Class cannot be cast to
    com.company.srm.state.State at com.company.srm.state.StateSpec.state
    abbreviation unique constraints check(StateSpec.groovy:105)
|Completed 3 unit tests, 3 failed in 0m 6s
.Tests FAILED
规格:

package com.company.srm.state

import static org.junit.Assert.*
import grails.test.mixin.*
import grails.test.mixin.support.*
import org.junit.*
import spock.lang.Specification

@TestFor(State)
@TestMixin(GrailsUnitTestMixin)
class StateSpec extends Specification {

    def 'state constraints check'() {

        when: "Validate uninitialized domain object"        
            boolean lResults = domain.validate()

        then: "There should be validation issues"
            !lResults
            domain.errors.getErrorCount() == 2
            domain.errors.getFieldError( 'name' )
            domain.errors.getFieldError( 'abbreviation' )

        when: "Populate attributes as required"
            domain.name = "Barvaria"
            domain.abbreviation = "BV"

        then: "Domain now passes validation"
            domain.validate()
    }
}
这个简单的测试失败了,而我们成功的测试在版本2.2.3中成功了。我们有50多个域类中的3个域类由于相同的错误而失败,任何人都可以向我们指出任何方向来理解它

这是域类

package com.company.srm.state

import com.company.srm.SrmBaseDomain;

class State extends SrmBaseDomain {

    String name
    String abbreviation
    CountryType country = CountryType.USA

    static mapping = {
        version false
        id generator: 'sequence', params: [sequence: 'state_seq']
        abbreviation column: 'state_code'
    }

    static constraints = {
        name( blank: false, unique: true )
        abbreviation( blank: false, unique: true, minSize: 2, maxSize: 4 )
    }

    String toString() {
        return "${name} (${abbreviation})"
    }
}

enum CountryType {
    USA, CAN
}
这是基类

package com.company.srm

import org.hibernate.Hibernate

// This is the base class for all SRM (not ODS) domain classes. It simply
// included the necessary audit columns and the logic to update them.

abstract class SrmBaseDomain implements Serializable {

    def sessionFactory
    def grailsApplication

    Date    dateCreated
    Date    lastUpdated
    String  createdBy
    String  updatedBy

    static constraints = {
        dateCreated(nullable: true)
        lastUpdated(nullable: true)
        createdBy(nullable: true)
        updatedBy(nullable: true)
    }

    def beforeInsert = {        
        createdBy = getUser()
        updatedBy = createdBy
    }

    def beforeUpdate = {
        updatedBy = getUser()
    }

    def getUser() {
        def username

        boolean hasPrincipal = !(grailsApplication.mainContext.springSecurityService.principal instanceof String)
        if( hasPrincipal ) {
            username = grailsApplication.mainContext.springSecurityService.principal?.username
        }
        else {
            username = username ?: com.company.acm.RequestUser.get('userName')
        }

        return username ?: 'srm.batchuser'
    }
}

什么是完整的stacktrace?你是说Grails2.4.3(标题是Groovy 2.4.3)?哪个Spock版本?您在测试中的
域设置在哪里?您不必这样做。它是Spock测试的注释(TestFor)的一部分。您是否有记录在哪里的链接?