【发布时间】:2014-12-16 21:45:31
【问题描述】:
在服务中有一个@PostConstruct,以确保已设置依赖项。依赖关系在resources.groovy 中设置。 @PostConstruct 断言上的单元测试失败。尝试在setUpSpec 中手动设置依赖项,但无济于事。即使没有@TestFor,ServiceUnitTestMixin 也会在@PostConstruct 上出现并愉快地窒息。
打开了一个缺陷GRAILS-11878,该缺陷立即关闭,并建议使用@FreshRuntime 和doWithSpring。如果他们真的费心去尝试,他们会得到以下错误:
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'
正在测试的服务:
@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.')
}
}
测试:
@FreshRuntime
@TestFor(MovieRipIndexService)
class MovieRipIndexServiceSpec extends Specification {
def doWithSpring = {
serviceHelper(ServiceHelper)
service.genres = serviceHelper.genres
service.includes = serviceHelper.includes
}
}
【问题讨论】:
标签: unit-testing grails dependencies postconstruct