简体   繁体   English

AspectJ + Junit + Maven + Java8

[英]AspectJ + Junit + Maven + Java8

I followed this SO question and tried to implement it for java8.我跟着this SO question并尝试为java8实现它。 My project is not a spring project.我的项目不是春季项目。

Aspect方面

@Aspect
public class MethodLogger {
    @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");
    }
}

JUnit Test JUnit 测试

@RunWith(JUnit4.class)
public class POSTaggerTest {    
    @Test
    public void test() {
        ...
    }
}

POM.xml POM文件

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.7</version>
        <configuration>
            <aspectLibraries>
                <aspectLibrary>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                </aspectLibrary>
            </aspectLibraries>
            <!--  java version -->
            <complianceLevel>1.8</complianceLevel>
            <source>1.8</source>
            <target>1.8</target>
            <!-- End :  java version -->
            <verbose>true</verbose>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>compile</goal>
                    <goal>test-compile</goal>
                </goals>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                </configuration>
            </execution>
        </executions>
    </plugin>
:
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjtools</artifactId>
    <version>1.8.6</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.2</version>
</dependency>

<dependency>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <type>maven-plugin</type>
</dependency>

I don't see any error.我没有看到任何错误。 Am I missing something?我错过了什么吗? Or any wrong artifact?或者任何错误的神器? What I want when I run junit tests, all the aspects whould work fine.当我运行 junit 测试时我想要什么,所有方面都可以正常工作。

The situation you want to recreate is like this:你要重现的情况是这样的:

  • There is one Maven module with aspects.有一个带有方面的 Maven 模块。 It is compiled with AspectJ Maven Plugin.它是用 AspectJ Maven 插件编译的。
  • There is another module with the actual application and test code.还有另一个包含实际应用程序和测试代码的模块。 It is also compiled with AspectJ Maven, this time referring to the first module as an aspect library.它也是用 AspectJ Maven 编译的,这次将第一个模块称为方面库。

What you are doing though is:你正在做的是:

  • You have a single module.您只有一个模块。 This is not a problem in and of itself because it is easily possible to keep aspects and Java code within the same module if you want the aspects applied on this module only.这本身不是问题,因为如果您希望方面仅应用于此模块,则很容易将方面和 Java 代码保存在同一模块中。
  • But now you declare JUnit as an aspect library.但是现在您将 JUnit 声明为一个方面库。 Why?为什么? It does not contain any aspects.它不包含任何方面。 You should remove that declaration.您应该删除该声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM