ste*_*son 11 java junit aspectj maven aspectj-maven-plugin
我已经在这里跟踪了几乎所有的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");
}
}
Run Code Online (Sandbox Code Playgroud)
非常简单.我想要做的就是在我的测试项目中每次执行任何测试方法之前和之后做一些事情@Test.
现在,我使用aspectj-maven-plugin我的<build>是这样的:
<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>
Run Code Online (Sandbox Code Playgroud)
1)我没有compile进球,<execution>因为我没有上课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>
Run Code Online (Sandbox Code Playgroud)
在我的<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))
Run Code Online (Sandbox Code Playgroud)
同样适用于后建议.
4)当我尝试使用版本1.7.3而不是1.6.11时,在加入点被处理时,此消息出现在我身上:expected 1.6.11 found 1.7.3.我想这是aspectj-maven-plugin
1.4版本的消息,我真的不知道1.5什么时候会发布以摆脱这个.哪些版本兼容?
5)我的"代码"看起来像这样:)
@RunWith(JUnit4.class)
public class TestClass() {
@Test
public void test01(){
}
}
Run Code Online (Sandbox Code Playgroud)
6)我有1.6.0_39 Oracle Java编译器,我用1.6编译所有内容(目标,源代码全部)
所以,方面被认可,应用,我将执行测试,mvn clean test但我得到的是:
java.lang.NoSuchMethodError:
my/aspect/AssertionAspect.aspectOf()Lmy/aspect/AssertionAspect;
Run Code Online (Sandbox Code Playgroud)
而且很长的堆栈跟踪.
我真的没有任何线索可能是错的.
ste*_*son 10
所以,诀窍是使用我的方面编译该库而不是使用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>
Run Code Online (Sandbox Code Playgroud)
您必须将此模块作为测试依赖项添加到您希望使用方面的目标模块中
目标模块
<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>
Run Code Online (Sandbox Code Playgroud)
你必须到处使用1.6级