簡體   English   中英

AspectJ + Junit + Maven - 在測試中識別切入點但NoSuchMethodError:拋出aspectOf()異常

[英]AspectJ + Junit + Maven - pointcut recognized in tests but NoSuchMethodError: aspectOf() exception thrown

我已經在這里跟蹤了幾乎所有的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)我在<execution>沒有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>

在我的<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 我想這是來自1.4版本的aspectj-maven-plugin的消息,我真的不知道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 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>

您必須將此模塊作為測試依賴項添加到您希望使用方面的目標模塊中

目標模塊

<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級

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM