![](/img/trans.png)
[英]Running AspectJ causes NoSuchMethodError: Aspect.aspectOf
[英]AspectJ + Junit + Maven - pointcut recognized in tests but NoSuchMethodError: aspectOf() exception thrown
我已经在这里跟踪了几乎所有的JUnit + Maven + AspectJ问题,甚至我很确定我已经正确设置了所有内容,我无法测试它。
我有一个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");
}
}
非常简单。 我想做的就是在我的测试项目中每次执行任何测试方法之前和之后做一些事情,该项目用@Test
注释。
现在,我在我的<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)我在<execution>
没有compile
目标,因为我在src/main/java
没有类(这是真的,没关系,我知道我在做什么)
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的更多信息。
3)我确信我的测试类被aspectj识别,因为我看到建议加入点。 我知道了:
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时,在加入点被处理时,此消息出现在我看来: expected 1.6.11 found 1.7.3
。 我想这是来自1.4版本的aspectj-maven-plugin
的消息,我真的不知道1.5什么时候会发布以摆脱这个。 哪些版本兼容?
5)我的“代码”看起来像这样:)
@RunWith(JUnit4.class)
public class TestClass() {
@Test
public void test01(){
}
}
6)我有1.6.0_39 Oracle Java编译器,我用1.6编译所有内容(目标,源代码全部)
所以,方面得到认可,应用,我将执行测试,如mvn clean test
但我得到的是:
java.lang.NoSuchMethodError:
my/aspect/AssertionAspect.aspectOf()Lmy/aspect/AssertionAspect;
而且很长的堆栈跟踪。
我真的没有任何线索可能是错的。
所以,诀窍是使用我的方面编译该库而不是使用javac而是使用ajc
(又名aspectj-maven-plugin)
而已。 我只需要将它添加到maven模块中的方面(它们在src / main / java中)
方面是注释,因此您必须具有1.6源/目标/合规级别
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>
您必须将此模块作为测试依赖项添加到您希望使用方面的目标模块中
目标模块
<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级
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.