0

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Fueled By Coffee
  • 2,467
  • 7
  • 29
  • 43
  • 1
    Either you forgot to post it or you are unaware of the fact that without the AspectJ compiler you will not get any aspect code woven into whatever target code you use. So you will need the AspectJ Maven plugin in order to do binary weaving on the Cucumber classes, re-packaging them into an aspect-enhanced version of your Cucumber JAR. Alternatively, you can use load-time weaving, but for that you need to start the JVM with a weaving agent on the command line. So which approach is yours? You did not mention that. – kriegaex Jun 10 '18 at 04:39
  • @kriegaex I wasn't aware that I needed top re-compile the jars. I'll go that route. – Fueled By Coffee Jun 10 '18 at 16:03
  • 1
    Well, somehow the aspect code needs to get woven with the target code. LTW is non-intrusive, you can use the original JARs. But then you need to be in control of your JVM command line in order to add `-javaagent:/path/to/aspectjweaver.jar`. If that is not an option, binary weaving is the next best thing. But then you need to be in control of how the 3rd party JAR gets deployed in the runtime environment. Dynamically attaching a weaver during runtime also is an option, but only if you can be sure that the attachment is taking place before the 3rd party code is loaded. – kriegaex Jun 11 '18 at 00:07
  • BTW, you do not recompile the 3rd party JARs from source code. Binary weaving works on class files, instrumenting them during the process. If my comments were helpful, let me know if you want me to turn them into an answer so as to close the question. – kriegaex Jun 11 '18 at 00:09
  • @kriegaex this was very helpful, could you please post an answer with the POM plugins and dependencies/configurations needed? – Fueled By Coffee Jun 14 '18 at 17:49
  • I would rather start from your project and fix it. Please publish it on GitHub (or if the code is classified, extract your problem into an [MCVE](http://stackoverflow.com/help/mcve) first), then I will take a look. There might be a simpler solution (e.g. just using `call()` instead of `execution()`) depending on your application code. But I need to see it first. – kriegaex Jun 16 '18 at 02:32

0 Answers0