grails 2.4.3 spock test,接收java.lang.ClassCastException

时间:2014-08-12 14:58:44

标签: grails groovy spock

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

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

|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个域类中有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'
    }
}

0 个答案:

没有答案