Java AspectJ+;Junit+;Maven-在测试中识别切入点,但无任何方法错误:引发aspectOf()异常
我已经关注了这里几乎所有的JUnit+Maven+AspectJ问题,即使我非常确定我已经正确地设置了一切,我也无法测试它 我有一个Maven模块,只有一个方面:Java AspectJ+;Junit+;Maven-在测试中识别切入点,但无任何方法错误:引发aspectOf()异常,java,maven,junit,aspectj,aspectj-maven-plugin,Java,Maven,Junit,Aspectj,Aspectj Maven Plugin,我已经关注了这里几乎所有的JUnit+Maven+AspectJ问题,即使我非常确定我已经正确地设置了一切,我也无法测试它 我有一个Maven模块,只有一个方面: @Aspect public class AssertionAspect { @Pointcut("execution(@org.junit.Test * *())") public void testMethodEntryPoint() {} @Before("testMethodEntryPoint()
@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
注释
现在,我在我的
中使用aspectj maven插件
,如下所示:
<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>
在我的
部分。没有更多关于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版时,在处理连接点时,我看到了这样一条消息:预期的1.6.11版找到了1.7.3版
。我猜这是来自aspectj maven插件的消息
对于1.4版,我真的不知道什么时候会发布1.5来解决这个问题。什么版本是兼容的
5) 我的“代码”如下所示:)
6) 我有1.6.0'Oracle Java编译器,我用1.6(目标、源..全部)编译所有东西
因此,在识别和应用方面,我将执行类似于mvn clean test
的测试,但我得到的只是:
java.lang.NoSuchMethodError:
my/aspect/AssertionAspect.aspectOf()Lmy/aspect/AssertionAspect;
还有很长的追踪记录
我真的不知道什么地方可能出错。所以,诀窍是用Aspect编译该库,而不是用javac,而是用ajc(又称aspectj maven插件) 就这样。我只需将其添加到带有方面的maven模块中(它们在src/main/java中) 特性是注释性的,因此您必须具有1.6源/目标/法规遵从性级别 ASPECTJ模块
<!-- 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>
org.codehaus.mojo
aspectj maven插件
1.4
1.6
1.6
1.6
真的
编译
然后,您必须将此模块作为测试依赖项添加到要使用方面的目标模块中
目标模块
<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>
org.codehaus.mojo
aspectj maven插件
1.4
那个文物
上述
1.6
1.6
1.6
测试编译
真的
你必须在所有地方使用1.6级这个老问题,但只适用于仍在挣扎的人。。。我必须运行
mvn compile
才能在intellij idea中解决这个问题。
<!-- 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>