1

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Igor Vlasuyk
  • 514
  • 2
  • 10
  • 22

1 Answers1

2

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Yes! Now all works. Load-time weaving agent was in maven-surefire-plugin because it was prescribed by allure-testng framework, it needs to make test reports. And after removing of this agent and dependency aspectjweaver in surefire-plugin, reports are still generating. If any one wants to add aspects to their test frameworks, please, take a look to my [pom.xml](https://github.com/igorek9191/AltarixTasks/blob/master/pom.xml) @kriegaex, thank you for your help, now I'm happy :) – Igor Vlasuyk Jun 16 '18 at 10:54