9

TestNG テスト メソッドから例外がスローされた後に実行されるアスペクトがあります。私は自分のspectjメソッドにTestメソッド名を入れたいと思っています。

これについて何か考えはありますか?以下の私のコードサンプルを見つけてください:

側面:

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

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

テスト:

@Test

public void testScenarioOne(){
    logger.debug("From Scenario One Test");
    Assert.assertEquals(true, false);
}
4

2 に答える 2

6

callポイントカット タイプを からに変更する必要がありますexecution

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

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

編集:@Test注釈付きのメソッドを具体的にインターセプトする方がよりクリーンになるかもしれません:

import org.testng.annotations;

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

    after() throwing (AssertionError e): testMethod() {
        System.out.println(thisJoinPointStaticPart.getSignature());
    }
}
于 2012-09-22T07:40:20.390 に答える
3

以下を使用できます。

thisJoinPoint.getSignature().getName()

ただし、テスト メソッドから直接例外をスローする必要があります。Assert.equals()テストメソッドではなく例外をスローしています。

于 2012-09-18T21:32:43.180 に答える