Fork me on GitHub

6 Feb 2011

Running Geb tests from your IDE

A frequent complaint about functional testing in Grails is that the start up / shut down cycle time of the app makes prototyping a functional test prohibitive. There has been some recent progress in this are with Luke Daley's Functional test development plugin but it would be really nice to be able to just run functional tests from inside your IDE in the same way as a similar unit test.

With Geb and IntelliJ Idea at least this is actually pretty straightforward (I'm sure the same thing is possible with Eclipse/STS I'm just not familiar enough with it).

Geb's dependence on the Grails lifecycle is pretty minimal so the tests can run without needing any magic to happen in any _Events scripts (unlike the Selenium RC plugin where this would be significantly more difficult). One extra step is required; the Grails Geb plugin normally picks up its base URL from Grails configuration and the test will not be running in the same JVM as the Grails app if it runs from the IDE so configuration will not be available. Just add the following method to your test that extends GebTests or GebSpec (or add it to a base class):

@Override String getBaseUrl() {
    super.baseUrl ?: "http://localhost:8080"
}
Now Geb will use the Grails configured base URL if it is in the same JVM as the app or default to http://localhost:8080 otherwise (obviously you need to change that default if that's not the host or port where your app runs).

You can then just run up the app from the command line, open the test in IntelliJ and hit Ctrl + Shift + F10 to run it. When the test completes the app is still running so you can make changes & run again with minimal turnaround time. Since the app is running in development mode you can make application changes without a server restart as well.

IntelliJ Idea's test integration is pretty good and it even groks Spock specifications. You can also debug and step through the test. The only downside is that you can't directly access domain classes or other application components in the test as they are only available from the app's JVM.

10 comments:

Stefan Armbruster said...

Is there any special configuration in Intellij required? I followed your setup. When using Ctrl+Shift+F10 the functional spock test class is being run 'internally' just like a normal unit test (aka inside the IDE and not via grails test-app).
The 'Run' option in the context menu does not have any further selection to the test inside the IDE or via grails test-app. This behaviour seems to be the same for all spock tests.
Any hints?

Regards,
Stefan

Rob said...

That's how I'm running the tests. It doesn't go through test-app which is why the baseUrl needs to be set.

Stefan Armbruster said...

I've fired up grails develop-functional-test and try to run the Spock-Geb test inside Intellij using Crtl+Shift+F10. This results in a stacktrace inside the testrunner window with an IncompatibleClassChangeError. See full stacktrace at http://pastebin.com/77Aqif5t

Rob said...

Hmm. I've not seen that before. Are IntelliJ's module dependencies in sync with Grails?

Leo said...
This comment has been removed by the author.
Leo said...
This comment has been removed by the author.
Donovan said...
This comment has been removed by the author.
Donovan said...

I tried this and it didn't work for me but pointed me in the right direction.

In Geb 0.6.0 neither GebSpec nor GebTest have a getBaseUrl() method. Instead they both have an internal browser object from which they get the baseUrl.

As such you have to set the baseUrl property of the browser instance from within your test. You will need to do this for every test so it makes sense to put it in the setup fixture method:

def setup() {
if (!browser.baseUrl) {
browser.baseUrl = "http://localhost:8080//"
}
}

Donovan said...

My URL keeps getting messed up, but basically it's localhost + the app name followed by a slash.

qinbincai123 said...

My soul is not satisfied that it hike81ai has lost her.My sight tries to find her
as though to bring her closer.
Cheap Snapback Hats
Snapback Hats Wholesale
Cheap New Era Hats
Wholesale Hats
New Era Fitted Hats
Cheap MLB Hats
Cheap NBA Hats
monster energy hats
Red Bull Hats
Superman Hats