Search code examples
grailsseleniumfunctional-testinggeb

accessing Spring beans from a Geb test


I'm using Geb to write some functional tests for a Grails app. Is there a way to access Spring beans from such tests. I tried the obvious:

import geb.junit4.GebReportingTest

class AuthenticatedUserTests extends GebReportingTest {

    UserService userService

    @Test
    void shouldCreateNewIdea() {

        // throws NullPointerException
        userService.doSomething()
    }
}

But it doesn't work. A few addtional details which may be relevant:

  • I'm using Grails 2.3.7
  • I've disabled forking
  • I'm running the tests with grails test-app functional:, i.e. without the -war option

Solution

  • When running functional tests, the running application is running in a completely separate JVM to the test, so you can’t simply access the beans of the running application because they’re not in the same JVM. There are various solutions to this problem, you could:

    a) Test the controller action that calls the service (ie something that is exposed over HTTP)

    b) If the service is just populating data you could use the fixtures plugin to populate that data for you (http://grails.org/plugin/fixtures) or populate the data in BootStrap only for the TEST environment

    c) Use a plugin like remote-control (http://grails.org/plugin/remote-control) to execute some code that runs on the server to do what you want