code-small.jpg

 

Cake Team Blogs

Using the very latest technologies, Cake Solutions helps organisations take innovative and disruptive ideas and develop them into reactive software solutions. Cake’s success is underpinned by our unique One Team® approach that allows for standalone projects or close collaboration with the clients’ technology teams. Cake provides mentors to guide the entire organisation through modern software engineering, continually transferring skills and knowledge to client teams in the process. This enables Cake to deliver not just software but to transform the way organisations think about and execute software delivery.

 

 

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