Sunday, June 8, 2008

Unit Testing AspectJ Load-time Weaving with Maven


This came up as a question on the Spring forums and I actually spent a bit of time trying to figure out how to get the unit test working properly when I did this originally. To unit test AspectJ Load-time Weaving (LTW), the surefire plugin needs to be configured to take the LTW agent. Which in this example is the Spring Agent. Also, the test needs to be run in it's own JVM so the javaagent argument can be applied.

The dependency on spring-agent can have it's scope set to 'provided'. It isn't needed as part of the build since it's used as a command line argument and Maven still downloads the jar so it can be referenced for the test in the local Maven repository.



Note: The argLine element should all be together on one line, but was separated to display in the blog entry.

<properties>
     <spring.version>2.5.4</spring.version>
</properties>

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-agent</artifactId>
     <version>${spring.version}</version>
     <scope>provided</scope>
</dependency>

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.4</version>
     <configuration>
          <forkMode>once</forkMode>
         <argLine>
         -javaagent:${settings.localRepository}
         /org/springframework/spring-agent/${spring.version}/
         spring-agent-${spring.version}.jar
         </argLine>
          <useSystemClassloader>true</useSystemClassloader>
     </configuration>
</plugin>

No comments: