2009-01-04

LoadTimeWeaver in unit tests with EclipseLink

Are you having the following issue in your units test when using Spring and Maven with JPA?

java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.

One solution that worked for me was to add the following in pom.xml:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <forkmode>pertest</forkmode>
        <argLine>-javaagent:${user.home}/.m2/repository/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar</argLine>
      </configuration>
    </plugin>
  </plugins>
  ...
</build>

In addition you have to make sure Maven has downloaded the artifact by adding this dependency:

<dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-agent</artifactid>
  <scope>test</scope>
</dependency>