DeliveryNinja
DeliveryNinja

Reputation: 827

AspectJ not weaving on test-compile but weaves on compile. Therefore unit tests throw ClassNotFoundException when running with maven

All my junit tests have been passing in eclipse, but when run from the console they seem to throw ClassNotFoundException for the AJ class RiskLogAspect.aj I can verify that in eclipse this is correctly weaving and providing the required functionality.

I've tried adding dependencies to my project to make sure there all correct.

I started adding the aspectj-maven-plugin

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
            <execution>
                <goals>
                    <goal>compile</goal>
                    <goal>test-compile</goal>
                </goals>
            <phase>process-sources</phase>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <encoding>UTF8</encoding>
                    <source>1.6</source>
                    <target>1.6</target>
                    <aspects>
                        <includes>
                            <include>com.idna.riskprofile.RiskLogAspect</include>
                        </includes>
                    </aspects>
                </configuration>
            </execution>
        </executions>
    </plugin>

I then added the following dependencies

      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.7</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.7</version>
    </dependency>

Any ideas what's going wrong ?

I'm running maven 2.0.9 and also jdK 1.6 update 21

After changing some of my configuration I've noticed in the console I get the follow " [Xlint:adviceDidNotMatch] " It seems like it is picking up my class but not applying the pointcut correctly.

For the goal compile it finds the AJ file and applies the advice correctly, for the test-compile goal it says Xlint:adviceDidNotMatch and throws class not found exceptions for half the junit tests. From the console:

[INFO] [aspectj:compile {execution: default}]
[INFO] Join point 'method-execution(java.lang.String com.idna.riskprofile.impl.RiskProfileEntryPointImpl.execute(com.idna.riskprofile.domain.RiskProfileRequest))' in Type 'com.idna.riskprofile.impl.RiskProfileEntryPointImpl' (RiskProfileEntryPointImpl.java:35) advised by around advice from 'com.idna.riskprofile.logging.aspects.RiskLogAspect' (RiskLogAspect.aj:35)
[INFO] [aspectj:test-compile {execution: default}]
[WARNING] advice defined in com.idna.riskprofile.logging.aspects.RiskLogAspect has not been applied [Xlint:adviceDidNotMatch]
[INFO] [resources:resources]

Updated: It seems that not weaving advice on the test-compile goal was not what was leading to the test failures on the console but another issue with spring configuration.

Upvotes: 3

Views: 4145

Answers (1)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298908

Registering the aspectj plugin isn't enough, you need to register some goals as well:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <!-- use this goal to weave all your main classes -->
                <goal>test-compile</goal>
                <!-- use this goal to weave all your test classes -->
            </goals>
        </execution>
    </executions>
</plugin>

Upvotes: 2

Related Questions