AspectJ Pointcut未被触发

时间:2018-06-08 13:50:36

标签: aop aspectj serenity-bdd thucydides cucumber-serenity

我试图连接使用AspectJ运行的Cucumber-Serenity库,但我的切入点似乎没有被触发。

SerenityAOP.java

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import gherkin.ast.Feature;
import gherkin.ast.ScenarioDefinition;
import net.thucydides.core.model.TestStep;
import net.thucydides.core.steps.StepEventBus;

@Aspect
public class SerenityAOP {    
    @Pointcut("execution(void cucumber.runtime.formatter.SerenityReporter.startOfScenarioLifeCycle(..))")
    public void beforeStartScenario() {}

    @Pointcut("execution(void cucumber.runtime.SerenityObjectFactory.stop())")
    public static void beforeStop(){}


    @Before("beforeStartScenario()")
    public void beforeStartScenarioAdvice(JoinPoint joinPoint) {
        Object[] signatureArgs = joinPoint.getArgs();

        Feature currentFeature = (Feature)signatureArgs[0];     
        String scenarioName = signatureArgs[1].toString();
        ScenarioDefinition scenarioDefinition = (ScenarioDefinition)signatureArgs[2];


        System.out.println("ASPECT: Starting test case for feature "+currentFeature+" with scneario: "+scenarioName);
    }

    @Before("beforeStop()")
    public void beforeStopAdvice() {
        boolean status = StepEventBus.getEventBus().aStepInTheCurrentTestHasFailed();
        String stepDescription = StepEventBus.getEventBus().getCurrentStep()
                .transform(TestStep::getDescription)
                .get();

        System.out.println("ASPECT: Testcase: "+stepDescription+" status = "+status);           
    }
}

相关依赖

     <dependency>
        <groupId>net.thucydides</groupId>
        <artifactId>thucydides-junit</artifactId>
        <version>0.9.275</version>
    </dependency>  
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.1</version>
    </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>3.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.6</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>net.serenity-bdd</groupId>
        <artifactId>serenity-core</artifactId>
        <version>${serenity.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>net.serenity-bdd</groupId>
        <artifactId>serenity-junit</artifactId>
        <version>${serenity.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>net.serenity-bdd</groupId>
        <artifactId>serenity-cucumber</artifactId>
        <version>${serenity.cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <scope>test</scope>
        <version>3.8.1</version>
    </dependency>   

但我在执行期间没有在任何地方看到我的印刷品。

我是否需要以某种方式挂钩这些内部库调用?或者我的切入选择器是错误的?

修改 在删除当前答案的情况下添加评论

使用黄瓜钩子,我无法检测到新的feature何时开始。我需要在每次开始时获取featurescenario的名称。我还没有办法用事件总线做到这一点。

0 个答案:

没有答案