tags:

views:

368

answers:

2

I'm trying to write an integration test for a Service which uses a gateway to send requests to a queue. The gateway is wired up to the queue using spring integration in resources.xml:

    <gateway 
            service-interface="WebRequestService" 
            id="webRequestGateway" 
            default-request-channel="queueChannel" />

Using the example by Russ Miles http://blog.springsource.com/2008/12/11/spring-integration-in-grails-part-1/, I was able to write a Controller integration test as the gateway comes in using DI.

However, when trying to do this with a Service integration test, I don't get the DI for the gateway, and can't initialise it as it's an interface.

Can I get access to the bean from within a Service integration test? Or is there a way of initialising it within the service?

A: 

You can get the application context the follow way:

import grails.test.*
import grails.spring.BeanBuilder

class FooBarTests extends GrailsUnitTestCase {
    boolean transactional = false

    void testSomething() {
        def bb = new BeanBuilder()
        bb.loadBeans("classpath:*.spring.resources.groovy")

        def applicationContext = bb.createApplicationContext()
    }
}
Sune Simonsen
I'm sorry, the application context seams to be empty :-(
Sune Simonsen
A: 

Indeed it is empty, Try this, following advice from Luke Daley on the grails mailing list :

In your test class, declare :

def grailsApplication

Then you get the application context like this : grailsApplication.mainContext

It works for me ;-)

Philippe