Search code examples
aopaspectjaspectj-maven-plugin

Maven testcases are not invoking AspectJ Aspects


I have added the Junit test case to call a method with Annotation which in turn have the Around aspect. When i run the mvn test command, The Aspect is not getting invoked for some reason.

But when i build the jar using mvn package and run the jar, the Aspects are getting invoked.

Here is the code - https://github.com/chandru-kumar/aop-example

Can anyone help on this? How can i make the Aspects invoked for mvn test?


Solution

  • Your tests would work if you would test against the application classes, which have been compiled by the AspectJ compiler. But you are trying to test the aspect against a test class. That is also possible, but in that case you need to tell AspectJ Maven to actually take care of compiling test classes, too:

    <execution>
      <goals>
        <goal>test-compile</goal>
        <goal>compile</goal>
      </goals>
    </execution>
    

    P.S.: Thanks for providing an MCVE. Most StackOverflow newbies forget that. It helped me to easily identify the problem, in this case without even cloning the repository. But sometimes it is not that simple, then I need to compile and run the example program. Anyway, keep up the good practice! 👍