Pradeep Maurya
Pradeep Maurya

Reputation: 414

Getting error, Spring BeanCreationException: Error creating bean with name 'validateableConstraintsEvaluator'

I have updated my project from grails2.5.6 to grails3.3.9 and successfully completed unit testing.
While running the integration test I am getting the following Exception.

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)

Controller

@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
    }
...
....
}

I am using Grails 3.3.9 with JDK 1.8, gormVersion 6.1.11 and gradle3.5

Upvotes: 4

Views: 737

Answers (1)

Najor
Najor

Reputation: 639

I my case I have a folder called acceptance-test where I had some integration test I wanted to execute. I created a acceptanceTest gradle task similar to this one integration-test.gradle.

If the task acceptanceTest was executed, the BeanCreationException was thrown. I had to manually active the spring profile dev

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

The root problem is that some grails global variables (like grailsApplication, messageSource) are not created and are need to run test. To run integration test these global variables must be created or mocked by Grails. Setting spring.profile.active to test creates all these global variables.

Upvotes: 0

Related Questions