我正在尝试为使用构造函数参数的服务类创建单元测试 . 我已经在grails-app / conf / spring / resources.groovy中配置了一个条目,允许Spring使用constructor参数创建bean .

运行服务类的集成测试没有问题,但是,运行使用@TestFor的单元测试时,会出现以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sampleService': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [ph.jojopad.SampleService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: ph.jojopad.SampleService.<init>()
    at grails.test.mixin.services.ServiceUnitTestMixin.mockService(ServiceUnitTestMixin.groovy:54)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [ph.jojopad.SampleService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: ph.jojopad.SampleService.<init>()
Caused by: java.lang.NoSuchMethodException: ph.jojopad.SampleService.<init>()

似乎@TestFor只期望一个使用默认构造函数的bean . 看一下Grails的文档,我找不到任何关于如何使用@TestFor for beans和构造函数参数的引用 . 我期待在grails-app / conf / spring / resources.groovy上使用正确的bean配置就可以了 .