Search code examples
maventestngaspectjaspectj-maven-plugin

How to run my java tests with AspectJ maven plugin


I want to log my tests through AOP. I have class AspectLogger where I have defined pointcut and advices. When I run aspectj:test-compile, in output I see that advices were added to tests. Like

[INFO] Join point 'method-execution(void Back_end_task.APITest.test())' in Type 'Back_end_task.APITest' (APITest.java:22) advised by before advice from 'AspectLogger' (AspectLogger.java:26)

But when I run mvn clean test in output I see errors like this:

java.lang.NoSuchMethodError: AspectLogger.aspectOf()LAspectLogger;
    at Back_end_task.APITest.test(APITest.java:23)

By searching of this problem, I have found that I should add my project as a dependency to <aspectLibraries> bloc of aspectj-maven-plugin, and add dependency of my project to <dependencies> bloc of pom.xml. But the problem in this:

[FATAL] 'dependencies.dependency MyTraining:project:1.0-SNAPSHOT' for MyTraining:project:1.0-SNAPSHOT is referencing itself

This is link to my project

Please give me advice. What I missed? What wrong I'm doing? I want to learn how to use aspects.


Solution

  • The problem in your build is that you use compile-time weaving, then run the tests with a load-time weaving agent. The latter is not necessary. This commit fixes your build:

    --- pom.xml (revision 8aa7b98f5c6c15676580783c2f351c253212fbee)
    +++ pom.xml (revision 72f37c4377b7189578f6afd5c45473efd8c63bc4)
    @@ -89,12 +89,6 @@
                 <version>${aspectj.version}</version>
             </dependency>
    
    -        <dependency>
    -            <groupId>org.aspectj</groupId>
    -            <artifactId>aspectjweaver</artifactId>
    -            <version>${aspectj.version}</version>
    -        </dependency>
    -
             <!--<dependency>
                 <groupId>MyTraining</groupId>
                 <artifactId>project</artifactId>
    @@ -156,9 +150,6 @@
                     <artifactId>maven-surefire-plugin</artifactId>
                     <version>2.20</version>
                     <configuration>
    -                    <argLine>
    -                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
    -                    </argLine>
                         <systemProperties>
                             <property>
                                 <name>allure.results.directory</name>
    @@ -166,13 +157,6 @@
                             </property>
                         </systemProperties>
                     </configuration>
    -                <dependencies>
    -                    <dependency>
    -                        <groupId>org.aspectj</groupId>
    -                        <artifactId>aspectjweaver</artifactId>
    -                        <version>${aspectj.version}</version>
    -                    </dependency>
    -                </dependencies>
                 </plugin>
    
                 <plugin>
    

    I have also improved some more little things in your POM in my GitHub fork and created a pull request for you. Just accept it if you like.