发生错误,Spring BeanCreationException:创建名称为'validateableConstraintsEvaluator'

时间:2019-03-07 13:50:24

标签: grails3

我已将项目从grails2.5.6更新为grails3.3.9,并成功完成了单元测试。
运行集成测试时,出现以下异常。

2019-03-07 18:26:12.169  WARN --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.spockframework.spring.SpringMockTestExecutionListener@157b7da6] to process 'before' execution of test method [public void com.mdw360.sec.UserControllerSpec.$spock_feature_0_1()] for test instance [com.mdw360.sec.UserControllerSpec@36f464d8]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validateableConstraintsEvaluator': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException: Cannot get property 'config' on null object
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:185)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1646)

控制器

@Transactional(readOnly = true)
class UserController {

    def userCache
    def springSecurityService
    def utilityService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]


    def create() {
        List<Role> roles = Role.list()
        Map roleMap = [:]
        roles.each { role -> roleMap[role] = false }
        render view: 'create', model: [userCO: new SaveUserCO(), roleMap: roleMap]
    }

......
......
}

TestClass

import grails.gorm.transactions.Rollback
import grails.testing.mixin.integration.Integration
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Specification

@Integration
@Rollback
class UserControllerSpec extends Specification {
    @Autowired
    UserController controller
}

    def cleanup() {
    }

    void "test create render correct view and model when no role exists"() {
        when:
        controller.request.method='POST'
        controller.create()

        then:
        assert controller.modelAndView.viewName == '/user/create'
        assert controller.modelAndView.model.size() == 2
        assert controller.modelAndView.model.roleMap.size() == 0
        assert controller.modelAndView.model.userCO != null
    }

    void "test create render correct view and model when role exists"() {
        when:
        controller.request.method='POST'
        createRole('ROLE_TEST')
        controller.create()

        then:
        assert controller.modelAndView.viewName == '/user/create'
        assert controller.modelAndView.model.size() == 2
        assert controller.modelAndView.model.roleMap.size() == 1
        assert controller.modelAndView.model.userCO != null
    }
...
....
}

我将Grails 3.3.9与JDK 1.8,gormVersion 6.1.11和 gradle3.5

1 个答案:

答案 0 :(得分:0)

我的情况是,我有一个名为acceptance-test的文件夹,在该文件夹中有一个要执行的集成测试。我创建了一个acceptanceTest类似于此integration-test.gradle的gradle任务。

如果执行了任务acceptanceTest,则抛出BeanCreationException。我必须手动激活弹簧配置文件dev

./gradlew acceptanceTest -Dspring.profiles.active=test

根本问题是一些grails全局变量(例如grailsApplicationmessageSource)未创建,需要运行测试。要运行集成测试,这些全局变量必须由Grails创建或模拟。将spring.profile.active设置为test会创建所有这些全局变量。