How to get the method name that thrown the exception in Java

You need to change your pointcut type from call to execution:

pointcut publicMethod(): execution(public * *(..));

after() throwing (AssertionError e): publicMethod() {
    System.out.println(thisJoinPointStaticPart.getSignature());
}

Edit: Maybe it would be even cleaner to specifically intercept @Test annotated methods:

import org.testng.annotations;

public aspect TestExceptionInterceptor {
    pointcut testMethod(): execution(@Test * *(..));

    after() throwing (AssertionError e): testMethod() {
        System.out.println(thisJoinPointStaticPart.getSignature());
    }
}

You can use:

thisJoinPoint.getSignature().getName()

although you will have to throw the exception directly from your test method. Assert.equals() is throwing the exception not your test method.

Tags:

Java

Aop

Aspectj