1

We have a series of unit tests and they were passing fine prior to me trying to add some aspects for dependency injection and logging duration of methods being called in our rest end points.

In the unit tests prior to the tests failing, we get two odd errors:

[AppClassLoader@14dad5dc] error aspect 'com.lutherconsulting.aphirm.log.DurationLoggingAspect' woven into 'com.lutherconsulting.aphirm.rest.ClientRest' must be defined to the weaver (placed on the aspectpath, or defined in an aop.xml file if using LTW).

and

[AppClassLoader@14dad5dc] error aspect 'com.lutherconsulting.aphirm.log.DurationLoggingAspect' woven into 'com.lutherconsulting.aphirm.log.DurationLoggingAspect' must be defined to the weaver (placed on the aspectpath, or defined in an aop.xml file if using LTW).

We are using the aspectj maven plugin to just let it autoweave the aspects into the web application. The configuration for that from our pom.xml for Maven is below.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
                <Xlint>ignore</Xlint>
                <encoding>UTF-8</encoding>
                <verbose>true</verbose>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>test-compile</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

The strange thing is that this all works perfectly fine if I build the war file and deploy it to a Tomcat instance, or if I run all of our cucumber feature tests. When I do either of those, The aspect weaves fine and I get data on the duration of the rest methods I annotated logged to a database correctly. If I run a specific test package from intelliJ or try to run all junit tests in intellij, it fails with those two errors

Is this something I'm just missing in Intellij as a run/debug configuration in the way it executes unit tests? I didn't think our structure of our app was different than any normal web app

- src
   | - main
      | - java
         | - packages
      | - resources
   | - test
      | - java
         | - packages
      | - resources
- pom.xml

I appreciate any ideas on

jspriggs
  • 403
  • 1
  • 5
  • 15
  • For me this just works, I just tried. Maybe you want to reimport your Maven project or just switch on Maven auto import in IDEA. I use compile-time weaving though. Do you use LTW? – kriegaex Feb 21 '16 at 12:34

2 Answers2

2

In the end, what turned out to fix this was to go into the project structure in IntelliJ, on the AspectJ settings there is a check box for Post-Compile Weave Mode. Checking this made sure weaving occurred in Intellij prior to the tests executing.

jspriggs
  • 403
  • 1
  • 5
  • 15
0

As far as I understand you are running your tests from IntelliJ using its own runner and not maven.

Therefore you have configured the weaver to be run using maven through the aspectj-maven-plugin. The problem is that your IntelliJ runner is not running maven, hence its weaver plugin is not being run either.

I can come up with an idea and you could run your maven test goal within IntelliJ to run all your tests with the maven configuration, so it will detect aspectj-maven-plugin and run the weaver too. Here you can check how to run maven goals: https://www.jetbrains.com/idea/help/executing-maven-goal.html

On the other hand, according to this link you have to enable Load Time Weaving in IntelliJ http://www.aspectprogrammer.org/blogs/adrian/2006/02/a_practical_gui_2.html

Quoting the link it says:

Open the "Run/Debug Configurations" dialog using the drop-down in the toolbar. Click the "+" icon to create a new configuration and name it e.g. "tests".

For this project, I've selected "All in package" and search for tests "In whole project".

Now all you have to do is add the VM startup parameter that brings in the AspectJ LTW agent:

-javaagent:lib/aspectjweaver.jar

The part after the ":" should be the path to your copy of aspectjweaver.jar. In this case, I've copied the aspectjweaver.jar from the Spring distribution into the lib directory of my project (it doesnt' need to be on the project's classpath). You can use the jar from AspectJ 5 final release too if you want to.

Also, you can check to configure AspectJ facet, check this link to read about it https://www.jetbrains.com/idea/help/aspectj.html

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • I'd been actively searching down that path as well. I'm still not finding yet how to configure intellij to run all of the tests with the maven configuration. Do you know how to do this or point me to documentation on how to set this up? – jspriggs Feb 19 '16 at 17:54
  • @jspriggs I've updated the answer, take a look at it – Federico Piazza Feb 19 '16 at 17:58