0

I followed this SO question and tried to implement it for 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

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

POM.xml

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

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90

1 Answers1

0

The situation you want to recreate is like this:

  • There is one Maven module with aspects. It is compiled with AspectJ Maven Plugin.
  • 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.

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.
  • But now you declare JUnit as an aspect library. Why? It does not contain any aspects. You should remove that declaration.
kriegaex
  • 63,017
  • 15
  • 111
  • 202