【问题标题】:AspectJ + Junit + Maven - pointcut recognized in tests but NoSuchMethodError: aspectOf() exception thrownAspectJ + Junit + Maven - 在测试中识别出切入点但 NoSuchMethodError: aspectOf() 抛出异常
【发布时间】:2013-07-12 05:07:16
【问题描述】:

我在这里关注了几乎所有 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) 我在&lt;execution&gt; 中没有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>

在我的&lt;dependencies&gt; 部分。没有更多关于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;

还有相当长的堆栈跟踪。

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

【问题讨论】:

    标签: java maven junit aspectj aspectj-maven-plugin


    【解决方案1】:

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

    就是这样。我只需要将其添加到带有方面的 maven 模块中(它们在 src/main/java 中)

    Aspects 包含注释,因此您必须拥有 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 级别

    【讨论】:

    • 老问题,但仅适用于仍在苦苦挣扎的人...我必须运行 mvn compile 才能在 intellij idea 中解决此问题。
    猜你喜欢
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多