java - AspectJ + Junit + Maven - 在测试中识别切入点但 NoSuchMethodError : aspectOf() exception thrown

标签 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()")
    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;

和相当长的堆栈跟踪。

我没有任何线索可能出了什么问题,真的。

最佳答案

所以,诀窍是用我的方面编译那个库,不是用 javac,而是用 ajc(又名 aspectj-maven-plugin)

就是这样。我只需要将它添加到具有方面的 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>

你必须将这个模块作为测试依赖添加到你想要使用方面的目标模块中

目标模块

<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 级别

关于java - AspectJ + Junit + Maven - 在测试中识别切入点但 NoSuchMethodError : aspectOf() exception thrown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17635535/

相关文章:

Java:如何从另一个字符串中删除存储为字符串的一组字母的所有出现?

java - 增加PermGen空间(Tomcat7、Windows Service)

maven - 如何强制 sonatype nexus 3 为 maven 存储库重新创建 maven-metadata.xml?

java - Maven 编译器插件使用自动 java 9 模块失败

java - 使用命令行如何运行 war 文件中的 junit 测试

java - 将一个 Java 类转换为不同的 Json 格式

java - 如何使用 camel 和 http-client 3 重现 javax.net.ssl.SSLHandshakeException 并修复它

java - 无法使用mockito对该方法中引发的异常进行单元测试

junit - JUnit 测试类中的 Javadoc?

java - JUnit 向测试用例添加额外测试