[英]AspectJ + Junit + Maven - pointcut recognized in tests but NoSuchMethodError: aspectOf() exception thrown
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. 我已经在这里跟踪了几乎所有的JUnit + Maven + AspectJ问题,甚至我很确定我已经正确设置了所有内容,我无法测试它。
I have a Maven module with just one aspect: 我有一个Maven模块只有一个方面:
@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
. 我想做的就是在我的测试项目中每次执行任何测试方法之前和之后做一些事情,该项目用
@Test
注释。
Now, I am using aspectj-maven-plugin
in my <build>
like this: 现在,我在我的
<build>
使用了aspectj-maven-plugin
,如下所示:
<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) 1)我在
<execution>
没有compile
目标,因为我在src/main/java
没有类(这是真的,没关系,我知道我在做什么)
2) I have 2)我有
<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. 在我的
<dependencies>
部分。 Nothing more regarding aspectj. 关于aspectj的更多信息。
3) I am sure my testing classes are recognized by aspectj because I see that join points were advised . 3)我确信我的测试类被aspectj识别,因为我看到建议加入点。 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
. 4)当我尝试使用版本1.7.3而不是1.6.11时,在加入点被处理时,此消息出现在我看来:
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. 我想这是来自1.4版本的
aspectj-maven-plugin
的消息,我真的不知道1.5什么时候会发布以摆脱这个。 What versions are compatible? 哪些版本兼容?
5) My "code" looks like this :) 5)我的“代码”看起来像这样:)
@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) 6)我有1.6.0_39 Oracle Java编译器,我用1.6编译所有内容(目标,源代码全部)
So, aspects recognized, applied, I am going to execute tests like mvn clean test
but all I get is that: 所以,方面得到认可,应用,我将执行测试,如
mvn clean test
但我得到的是:
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. 我真的没有任何线索可能是错的。
So, the trick was to compile that library with my aspects not with javac but with ajc
(aka aspectj-maven-plugin) 所以,诀窍是使用我的方面编译该库而不是使用javac而是使用
ajc
(又名aspectj-maven-plugin)
That's it. 而已。 I just had to add this into the maven module with aspects (they are in src/main/java)
我只需要将它添加到maven模块中的方面(它们在src / main / java中)
Aspects are annotation ridden so you have to have 1.6 source/target/compliance level 方面是注释,因此您必须具有1.6源/目标/合规级别
ASPECTJ MODULE 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 你必须到处使用1.6级
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.