如何获取在Java中抛出异常的方法名称

roo*_*07r 7 java aspectj

我有一个方面,在我的TestNG测试方法抛出异常后运行.我想将Test方法名称放入我的aspectj方法中.

有什么想法吗?请在下面找到我的代码示例:

方面:

pointcut publicCall(): call(public * *(..));

after() throwing (AssertionError e): publicCall() {
    logger.debug("Assertion Error thrown");
    System.out.println("Threw an exception: " + e);
}
Run Code Online (Sandbox Code Playgroud)

测试:

@Test

public void testScenarioOne(){
    logger.debug("From Scenario One Test");
    Assert.assertEquals(true, false);
}
Run Code Online (Sandbox Code Playgroud)

kri*_*aex 5

您需要将切入点类型更改callexecution:

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

after() throwing (AssertionError e): publicMethod() {
    System.out.println(thisJoinPointStaticPart.getSignature());
}
Run Code Online (Sandbox Code Playgroud)

编辑:也许它可以更明确地拦截带@Test注释的方法:

import org.testng.annotations;

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

    after() throwing (AssertionError e): testMethod() {
        System.out.println(thisJoinPointStaticPart.getSignature());
    }
}
Run Code Online (Sandbox Code Playgroud)