Freitag, 29. Mai 2009

Debugging Grails Integration Tests

Running all unit/integration tests consumes a lot of time, so most of the time during development, you probably run single integration tests from the command line:

grails test-app -integration ClassUnderTest

If you want to debug an integration test to find out why a test is failing, follow the steps described below:

In IntelliJ, do the following:

  • Create new run configuration (Edit configurations -> Add new configuration -> Remote)
  • Enter name for the configuration
  • Deselect "Make"
  • Set brakepoint in your test class to make the debugger stop at this point.

Using the command line, run the single integration test using "grails-debug" instead of "grails". This will start grails in debugging mode (it will open a remote debug port).

grails-debug test-app -integration ClassUnderTest

Run your remote debugging session in IntelliJ by pressing on the debug button next to your run config...

If you use a different IDE like Eclipse or Netbeans, remote debugging should be as easy as described above.

Montag, 18. Mai 2009

Custom Webtest steps for the Grails Webtest Plugin

Since version 0.6 of the grails webtest plugin, it is possible to create custom steps (http://www.leebutts.com/2009/01/grails-webtest-plugin-06-released.html):
"You can now extend com.canoo.webtest.steps.Step by placing groovy classes in webtest/tests/step. They will be automatically loaded at runtime and allow for easy testing of complicated scenarios such as JSON interfaces and email integration"
Unfortunately, there is no description or example how to actually create a custom step.

So here is what you have to do:
  • Create directory webtest/tests/step
  • Create Step Class. It is important that the class name ends with 'Step', otherwise it will not be picked up.
  • Class must extend com.canoo.webtest.steps.Step
  • Class must override void doExecute()
  • Put the step's execution logic into the doExecute() method.
  • To pass parameters to the step, define the step's arguments as properties of the step class.
  • To validate the parameters, override void verifyParameters() and check the properties you defined. If property is not valid, throw IllegalArgumentException.

Here is an example of a custom step. The step will log a greeting message. Our goal is to create a step that can be called in a webtest test class as shown below:
def testGreeting() {
 greeting(param:'grails')
}

The implementation of the step is quite simple:
import org.apache.log4j.Logger

class GreetingStep extends com.canoo.webtest.steps.Step {
 private static Logger log = Logger.getLogger(GreetingStep)
 String param

 @Override
 void doExecute(){
  log.info "Hello $param"
 }

 @Override
 protected void verifyParameters(){
  if(!param) {
   throw new IllegalArgumentException('param not set')
  }
 }
}

Place GreetingStep class in directory webtest/tests/step.