Configuring Surefire Plugin with AspectJ load-time weaving
Executing unit tests with AspectJ load-time weaving
enabled requires configuring maven-surefire-plugin
to add a -javaagent
option to the
JVM arguments that sets up the aspectjweaver
library as a JVM agent. This use case is similar to
configuring endorsed libraries, and the necessary configuration looks like this:
<plugin>
<groupId>com.github.veithen.alta</groupId>
<artifactId>alta-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate-properties</goal>
</goals>
<configuration>
<name>aspectjweaver</name>
<value>%file%</value>
<artifactSet>
<artifacts>
<artifact>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</artifact>
</artifacts>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${aspectjweaver}</argLine>
</configuration>
</plugin>
The plugin configuration shown above doesn't specify the version of the org.aspectj:aspectjweaver
artifact.
In this case, the plugin will determine the version from the dependencies or the dependency management configuration
of the project (This feature is supported starting with version 0.3 of the plugin). For AspectJ you would typically
add the following configuration to the project or its parent POM, in order to select compatible versions
of the weaver and the AspectJ runtime library (the latter being required as a test dependency of the project):
<properties>
<aspectj.version>1.9.22.1</aspectj.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</dependencyManagement>