0

i've one service which is extending base service and in base service one more service is wired with @Autowired annotation because of that grails service unit tests are not passing

class UserService extends BaseService{

    def getUser() {
    def u = [:]
    u.name='jason'
    return u
    }
}



import org.springframework.beans.factory.annotation.Autowired

class BaseService {
    @Autowired(required = true)
    UtilService utilService
}


import grails.test.mixin.*
import org.junit.*

@TestFor(UserService)
class UserServiceTests {
   void testSomething() {
       fail "Implement me"
   }
}

while running $grails test-app i'm getting below error

    | Failure:  testSomething(com.krish.web.service.UserServiceTests)
    |  org.springframework.beans.factory.BeanCreationException: Error creating bean with     name 'userService': Injection of autowired dependencies failed; nested exce
ption is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.krish.web.service.UtilService ProcurementBase
Service.utilService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.krish.web.ser
vice.UtilService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.spring
framework.beans.factory.annotation.Autowired(required=true)}
        at grails.test.mixin.services.ServiceUnitTestMixin.mockService(ServiceUnitTestMixin.groovy:54)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.krish.web.service.UtilService Ba
seService.utilService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.krish.web.s
ervice.UtilService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.spri
ngframework.beans.factory.annotation.Autowired(required=true)}
        ... 1 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.krsih.web.service.UtilService] found for de
pendency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annota
tion.Autowired(required=true)}

grails version is 2.1.0

can anyone help how to wire beans for unit test?

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
Krishna Gangaraju
  • 519
  • 1
  • 6
  • 12

2 Answers2

2

It may be too late now, but I ran into the same issue. The problem is that you use @Autowired within a Grails service. In a Grails service (or controller), every member that you define will be injected automatically on startup if the bean name matches the member variable name.

If you use def, it will only match the beanName, if you declare a type it will also match the class. Depending on your IDE support -- and your programming style -- it may be better to use def or an explicit type.

I presume if you use @Autowired, the framework has to find the bean in the application context, otherwise an exception is thrown. In unit test environment, your Grails default beans and your beans in resources.groovy are not added on startup. The framework tries to autowire your beans, but there are not found in the test context. Because you youse @Autowired, an exception is thrown.

Leave out @Autowired when injecting beans in services or controllers. Then it will start up also in unit test environment. Note, though, that the beans are still not there, you just don't get the exception. So all your injected members will be null and you have to set this up manually in your unit tests using a defineBeans closure.

Stackoverflow answer on how to use defineBeans within a unit test

When injecting in classes outside of grails-app (e.g. src/groovy), however, you have to always use @Autowired for injection. As far as I'm concerned, here it will not cause problems when unit testing. Those custom beans also need to be setup using defineBeans in a unit test environment.

Grails Version: 2.4.2

Java Version: 1.8_31

Community
  • 1
  • 1
nst1nctz
  • 333
  • 3
  • 23
0

Unit tests are deprived of dependency injection. You can mock utilService by adding

@Mock(UtilService)

in the test class at class level.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117