Have a @PostConstruct
in the service to ensure that the dependencies have been set up. Dependencies are set in resources.groovy
. Unit test fails on @PostConstruct
asserts. Tried setting up the dependencies manually in setUpSpec
to no avail. Even without a @TestFor
, ServiceUnitTestMixin
kicks in and merrily chokes on @PostConstruct
.
Opened a defect GRAILS-11878 which was closed promptly with an advice to use @FreshRuntime
and doWithSpring
. If they actually bothered to try, they'd have gotten the following error:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'grails.spring.BeanBuilder$ConfigurableRuntimeBeanReference$WrappedPropertyValue@2cce10bc' with class 'grails.spring.BeanBuilder$ConfigurableRuntimeBeanReference$WrappedPropertyValue' to class 'java.util.Collection'
Service under test:
@Transactional
class MovieRipIndexService {
Collection<String> genres
Collection<String> includes
@PostConstruct
void postConstruct() {
notEmpty(genres as Collection, 'Genres must not be null or empty.')
notEmpty(includes as Collection, 'Includes must not be null or empty.')
}
}
Test:
@FreshRuntime
@TestFor(MovieRipIndexService)
class MovieRipIndexServiceSpec extends Specification {
def doWithSpring = {
serviceHelper(ServiceHelper)
service.genres = serviceHelper.genres
service.includes = serviceHelper.includes
}
}