0

我有一个组件

@Component
class SomeComponent {

    @Autowired
    GrailsApplication grailsApplication

    String buildString(String someInfo) {
       return "${grailsApplication.config.my.string}${someInfo}"
    } 
}

和一个单元测试

@TestMixin(ControllerUnitTestMixin)
class SomeComponent Tests {


@Test
void test() {
   SomeComponent component = new SomeComponent()

   component.grailsApplication = new Expando()
   component.grailsApplication.config = [config: [my: [string: 'FOO']]]

    assert component.buildString('BAR') == 'FOOBAR'
    }
}

当我执行我的测试时,我得到了这个错误:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{}' with class 'groovy.util.Expando' to class 'org.codehaus.groovy.grails.commons.GrailsApplication'

那么最好的方法是什么?

4

1 回答 1

0

这是我如何在不使用 def 的情况下修复它:

SomeComponent component = new SomeComponent(grailsApplication: grailsApplication)
component.grailsApplication.config.my.string = 'FOO'
于 2013-02-21T20:54:04.703 回答