Posts mit dem Label Grails werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Grails werden angezeigt. Alle Posts anzeigen

Donnerstag, 13. Mai 2010

Using Spring AOP with Grails Services

If you try to work with Spring AOP and service classes in a Grails application, you will probably end up with an exception like:

Error executing script TestApp: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'testService':
Invocation of init method failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy11]: Common causes of this problem include using a final class or a non-visible class;
nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy11


To solve this problem you have to add the following line to your resources.xml (in grails-app/conf/spring):
<aop:aspectj-autoproxy proxy-target-class="true"/>

Your final resources.xml will look like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 <aop:aspectj-autoproxy proxy-target-class="true"/> 
 
</beans>


This was tested with Grails 1.2.2 and Grails 1.3.0.

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.

Samstag, 21. März 2009

My Java & Grails Blog has finally come to life!

This is the first post in my new blog. I'm going to blog on Groovy and Grails related stuff, as well as interesting Java EE technologies.