0

I have a Component

@Component
class SomeComponent {

    @Autowired
    GrailsApplication grailsApplication

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

And a unit test

@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'
    }
}

When I execute my test I got this error:

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

So what's the best way to do this?

Thermech
  • 4,371
  • 2
  • 39
  • 60

1 Answers1

0

Here how I fix it without using a def:

SomeComponent component = new SomeComponent(grailsApplication: grailsApplication)
component.grailsApplication.config.my.string = 'FOO'
Thermech
  • 4,371
  • 2
  • 39
  • 60
  • 1
    ControllerUnitTestMixin already declare a grailsApplication mock, you can use that directly instead of creating a new mock. –  Feb 22 '13 at 14:56