I am trying to learn AspectJ and figuring out how to retrieve specific joinpoints at certain points in a flow. My example is something like this:
I want to run an unit test annotated with JUnit's @Test
then any methods called in that test that may be in another class annotated with another annotation, let's say @Example
, then I could access basically the entire flow at those certain points so I had the ability to get the class name/method name of the test annotated with @Test
and then also get the method information for the method annotated @Example
. I've included some example code for clarification:
Test class:
public class ExampleTest {
@Test
public void shouldTestSomething() {
ExampleClass exampleClazz = new ExampleClass();
exampleClazz.something();
// ...
}
POJO:
public class ExampleClass {
@Example
public void something() {
// do something
end
end
So with these classes, I would like to make an aspect that would basically find any kind of @Example
called within a @Test
so I can then have access to both (or more) join points where I can grab the method/class signatures from the AspectJ JoinPoint
object.
I tried something like this:
@Aspect
public class ExampleAspect {
@Pointcut("execution(@org.junit.Test * *(..))
&& !within(ExampleAspect)")
public void testPointCut() {}
@Pointcut("@annotation(com.example.Example)")
public void examplePointCut() {}
@After("cflow(testPointCut) && examplePointCut()")
public void doSomething(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature());
}
}
But the output looks like this:
void ExampleTest.ExampleClass.something()
The main issue is that it is missing the name of the test method (shouldTestSomething()) in the signature. What would be the best way to retrieve that?