AspectJ + Junit + Maven - 在测试中识别切入点但NoSuchMethodError:aspectOf()异常抛出 [英] AspectJ + Junit + Maven - pointcut recognized in tests but NoSuchMethodError: aspectOf() exception thrown
问题描述
我已经跟踪了几乎所有的JUnit + Maven + AspectJ问题,甚至我很确定我已经设置好一切,我无法测试它。
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.
I只有一个方面有一个Maven模块:
I have a Maven module with just one aspect:
@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");
}
}
非常简单。我想做的就是在我的测试项目中每次执行任何测试方法之前和之后做一些事情,用@ code> @Test 进行注释。
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
.
现在,我在我的< build>
中使用 aspectj-maven-plugin
这个:
Now, I am using aspectj-maven-plugin
in my <build>
like this:
<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)我没有编译
目标< execution>
因为我在 src / main / java
中没有课程(这是真的,没关系,我知道我在做什么)
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)
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>
在我的< dependencies>
部分。关于aspectj的更多信息。
in my <dependencies>
section. Nothing more regarding aspectj.
3)我确信我的测试类被aspectj识别,因为我看到建议加入点。我看到:
3) I am sure my testing classes are recognized by aspectj because I see that join points were advised . 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))
同样适用于建议后。
4)当我尝试1.7.3而不是1.6.11时,此消息在加入点被处理时出现在我面前:预期1.6.11发现1.7.3
。我想这是来自版本1.4的 aspectj-maven-plugin
的消息,我真的不知道1.5什么时候会发布以摆脱这个。哪些版本兼容?
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
. 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. What versions are compatible?
5)我的代码如下所示:)
5) My "code" looks like this :)
@RunWith(JUnit4.class)
public class TestClass() {
@Test
public void test01(){
}
}
6)我有1.6.0_39 Oracle Java编译器,我用1.6编译所有内容(目标,来源..所有)
6) I have 1.6.0_39 Oracle Java compiler, I am compiling everything with 1.6 (target, source .. all)
因此,方面得到认可,应用,我将执行测试,如 mvn clean test
但我得到的是:
So, aspects recognized, applied, I am going to execute tests like mvn clean test
but all I get is that:
java.lang.NoSuchMethodError:
my/aspect/AssertionAspect.aspectOf()Lmy/aspect/AssertionAspect;
和相当长的堆栈跟踪。
I没有任何线索什么可能是错的,真的。
I do not have any clue what might be wrong, really.
推荐答案
所以,诀窍是使用我的方面编译该库,而不是使用javac但使用 ajc
(又名aspectj-maven-plugin)
So, the trick was to compile that library with my aspects not with javac but with ajc
(aka aspectj-maven-plugin)
这就是它。我只需要将它添加到带有方面的maven模块中(它们位于src / main / java中)
That's it. I just had to add this into the maven module with aspects (they are in src/main/java)
方面是注释,所以你必须拥有1.6来源/目标/合规水平
Aspects are annotation ridden so you have to have 1.6 source/target/compliance level
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
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>
你必须在任何地方使用1.6级
You have to use 1.6 level everywhere
这篇关于AspectJ + Junit + Maven - 在测试中识别切入点但NoSuchMethodError:aspectOf()异常抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!