AspectJ + Junit + Maven - pointcut recognized in tests but NoSuchMethodError: aspectOf() exception thrown
Asked Answered
T

1

12

I have followed almost all JUnit + Maven + AspectJ questions here and even I am pretty sure I have everything set properly, I am unable to test it.

I have a Maven module with just one aspect:

@Aspect
public class AssertionAspect {

    @Pointcut("execution(@org.junit.Test * *())")
    public void testMethodEntryPoint() {}

    @Before("testMethodEntryPoint()")
    public void executeBeforeEnteringTestMethod() {
        System.out.println("EXECUTE ACTION BEFORE ENTERING TEST METHOD");
    }

    @After("testMethodEntryPoint()")
    public void executeAfterEnteringTestMethod() {
        System.out.println("EXECUTE ACTION AFTER ENTERING TEST METHOD");
    }
}

Very pretty simple. All I want to do is to do something before and after each execution of any test method in my test project which is annotated with @Test.

Now, I am using aspectj-maven-plugin in my <build> like this:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.4</version>
      <configuration>
        <aspectLibraries>
          <aspectLibrary>
            <groupId>my.package</groupId>
            <artifactId>with-aspects</artifactId>
          </aspectLibrary>
        </aspectLibraries>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>test-compile</goal>
          </goals>
          <configuration>
            <showWeaveInfo>true</showWeaveInfo>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

1) I have no compile goal in <execution> because I have no classes in src/main/java (that's true and it is ok, I know what I am doing)

2) I have

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

in my <dependencies> section. Nothing more regarding aspectj.

3) I am sure my testing classes are recognized by aspectj because I see that join points were advised . I see:

Join point 'method-execution(xyz)' in Type 'blabla' 
(AppTestCase.java:124) advised by before advice from 
'blabla' (my-aspects jar.jar!AssertionAspect.class(from AssertionAspect.java))

Same holds for after advice.

4) when I tried version 1.7.3 instead of 1.6.11, this message appeared to me while join points were treated: expected 1.6.11 found 1.7.3. I guess it is a message from aspectj-maven-plugin of version 1.4, I do not really know when 1.5 will be release to get rid of this. What versions are compatible?

5) My "code" looks like this :)

@RunWith(JUnit4.class)
public class TestClass() {

    @Test
    public void test01(){
    }
}

6) I have 1.6.0_39 Oracle Java compiler, I am compiling everything with 1.6 (target, source .. all)

So, aspects recognized, applied, I am going to execute tests like mvn clean test but all I get is that:

java.lang.NoSuchMethodError: 
my/aspect/AssertionAspect.aspectOf()Lmy/aspect/AssertionAspect;

and pretty long stacktrace.

I do not have any clue what might be wrong, really.

Twice answered 14/7, 2013 at 0:49 Comment(0)
T
10

So, the trick was to compile that library with my aspects not with javac but with ajc (aka aspectj-maven-plugin)

That's it. I just had to add this into the maven module with aspects (they are in src/main/java)

Aspects are annotation ridden so you have to have 1.6 source/target/compliance level

ASPECTJ MODULE

<!-- Build -->
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <complianceLevel>1.6</complianceLevel>
                <verbose>true</verbose>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
            </dependencies>
        </plugin>
    </plugins>
</build>

Than you have to add this module as a test dependency into your target module you want to use aspects with

TARGET MODULE

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>that-artifact</groupId>
                        <artifactId>mentioned-above</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <source>1.6</source>
                <target>1.6</target>
                <complianceLevel>1.6</complianceLevel>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>test-compile</goal>
                     </goals>
                     <configuration>
                         <showWeaveInfo>true</showWeaveInfo>
                     </configuration>
                 </execution>
             </executions>
         </plugin>
     </plugins>
 </build>

You have to use 1.6 level everywhere

Twice answered 14/7, 2013 at 14:19 Comment(1)
Old question but just for anyone still struggling... I had to run mvn compile to fix this in intellij idea.Yama

© 2022 - 2024 — McMap. All rights reserved.