我已经在这里跟踪了几乎所有的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
。
现在,我正在使用 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>
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>
在我的 <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
。我想这是来自的消息 aspectj-maven-plugin
版本1.4,我真的不知道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;
和很长的堆栈跟踪。
我没有 任何 真的,这可能是错的。