code-small.jpg

Cake Team Blogs

Specs2 and Spring applications

Posted by Jan Machacek

Find me on:

14/06/12 10:40

I have just spent hours fighting testing Spring applications in Specs2--specifically, Spring applications that use AspectJ load-time weaving with the <context:load-time-weaver /> element in the Spring context configuration file. In short, the specs just hung when the implementations of the LoadTimeWeaver are created.

If you construct Spring ClassPathXmlApplicationContext or GenericXmlApplicationContext from the Spring context file that contains just

&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="..."&gt;

    &lt;context:load-time-weaver /&gt;

&lt;/beans&gt;

In a specification that simply starts the application context:

class MySpec extends Specification {
  "spring context loading hangs" in {
    new GenericXmlApplicationContext(
      "classpath*:/META-INF/spring/module-context.xml")

    // never gets here
    success
  }
}

With AspectJ 1.6.12, the weaver got stuck on synchronized (yeah, that keyword!) put operation on a java.util.Map. To resolve the situation, you must include META-INF/aop.xml file, where you configure the AspectJ weaver to only weave within the classes that make up your application. In other words, the problem was caused by weaving the code in all classes! So, the META-INF/aop.xml file should look like this:

&lt;aspectj&gt;
    &lt;weaver options="-XnoInline -Xreweavable -verbose -debug -showWeaveInfo"&gt;
        &lt;include within="org.cakesolutions.yourapp..*"/&gt;
    &lt;/weaver&gt;
&lt;/aspectj&gt;

The important bit is the <include within="org.cakesolutions.yourapp..*"/>. It instructs the weaver to only weave the aspects in the classes that are in a package whose name starts with org.cakesolutions.yourapp. (The options="-XnoInline -Xreweavable -verbose -debug -showWeaveInfo" are for eyeball testing, you will want to remove those as soon as possible.)

With all this combined, your code gets woven and your specs run as you expect.

Topics: Specs2, spring

Posts by Topic

see all

Subscribe to Email Updates