I'm using Gradle 7.4 and applying the io.freefair.aspectj.post-compile-weaving
plugin. I want to test my aspect so I wrote a test class including an static inner class which should be advised by my aspect but my aspect does not advise it.
I also wrote another class which should be advised in src/main/java
directory and my aspect properly advise it.
For example I have following aspect, classes and tests in package com.example
.
- aspect class.
@Aspect
public class MyAspect {
@AfterReturning("@within(com.example.ShouldBeWoven) && execution(* .new(..))")
public void myAdvice(JoinPoint joinPoint) {
System.out.println("******* MyAspect advised: " + joinPoint.getSignature().getDeclaringTypeName());
}
}
- marker annotation
@Documented
@Target({TYPE})
@Retention(RUNTIME)
public @interface ShouldBeWoven {
}
- target static inner class in
main
source set
class MyClass {
@ShouldBeWoven
static class WovenClassInMain {
}
}
- target static inner class and test code in
test
source set
class MyAspectTest {
@ShouldBeWoven
static class WovenClassInTest {
}
@Test
void instantiateWovenClassInMain() {
new WovenClassInMain();
}
@Test
void instantiateWovenClassInTest() {
new WovenClassInTest();
}
}
The result was that instantiateWovenClassInMain
showed message in the advice and instantiateWovenClassInTest
did not. How can I weave my aspects to classes in src/test/java
directory?
I could achieve this when I was using Load Time Weaving
by the way.
Here is an MCVE.