AspectJ Pointcut not being triggered

552 Views Asked by At

I'm trying to hook into Cucumber-Serenity libraries that are running using AspectJ but my pointcuts don't seem to be getting triggered.

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);           
    }
}

Relevant Dependencies:

     <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>   

But I'm not seeing my printlines anywhere during execution.

Is there some way I need to hook into these internal library calls differently? Or are my pointcut selectors wrong?

EDIT: Adding a comment in case the current answer gets removed

With the cucumber hooks I'm not able to detect when a new feature is started. I need to get the name of the feature and scenario when they each start. I haven't found a way to do that with event bus.

0

There are 0 best solutions below