2

I am trying to use aspectJ with cucumber project to add conditional statements with cucumber(you may think why.. but I am). It intercepts the cucumber step definitions that I have in my current project but I have dependency jars as well that I want to weave, I am weaving it using aspectj-maven plugin but then my code is not able to use glue code in this dependency, I believe that is because the code has been modified due to weaving. How should I be doing it instead?

this is maven-aspectj plugin:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.8</version>
        <configuration>
           <complianceLevel>1.8</complianceLevel>
           <source>1.8</source>
           <target>1.8</target>
           <weaveDependencies>
              <weaveDependency>
                 <groupId>com.tsb.gen</groupId>
                 <artifactId>gen-selenium</artifactId>
              </weaveDependency>
           </weaveDependencies>
        </configuration>
        <executions>
           <execution>
              <goals>
                  <goal>compile</goal>
                 <goal>test-compile</goal>
              </goals>
           </execution>
        </executions>
        <dependencies>
           <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjrt</artifactId>
              <version>${aspectj.version}</version>
           </dependency>
           <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjtools</artifactId>
              <version>${aspectj.version}</version>
           </dependency>
        </dependencies>
     </plugin>

the dependency:

<groupId>com.tsb.gen</groupId>
<artifactId>gen-selenium</artifactId>

is also used by my glue code that I have in my project. If I remove , then I am able to use the glue code and anything else thats in my project with aspectj but I cant use anything thats inside this base dependency. i want to use that.

My Aspect is like:

@Around("execution(* *(..)) && " +
        "( @annotation(cucumber.api.java.en.And) " +
        "|| @annotation(cucumber.api.java.en.But) " +
        "|| @annotation(cucumber.api.java.en.Given) " +
        "|| @annotation(cucumber.api.java.en.Then) " +
        "|| @annotation(cucumber.api.java.en.When) " +
        ")")
public Object aroundGlueMethod(ProceedingJoinPoint joinPoint) throws Throwable {
    ScenarioContext.checkForProgressDisplay();
    //ScenarioContext.logToGenieReport("aroundGlueMethod aspect");
    //System.out.println("\"aroundGlueMethod aspect\"");
    Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
    Object[] args;
    Object obj=null;

    // process arguments
    args = processArgs(joinPoint.getArgs(),joinPoint.getTarget().getClass().getName());
    if (methodHasAnnotation(method, SkipConditionalChecking.class)) {
        obj=joinPoint.proceed(args);
    } else {
        if (ScenarioContext.shouldNextStepGetExecuted()) {
            obj=joinPoint.proceed(args);
        } else {
            writeMessageToCurrentScenario("skipped as condition is false");
            logger.info("step skipped as condition is false: {}", joinPoint.getSignature());
        }
    }
    return obj;

}

What am trying to do with cucumber is something like :

 When If variable '${name}' equals to 'myname'
    Then print 'Hey its me'
 EndIf

I am able to work it out in the things that belong to my project, but when I am trying to include the step definitions or glue code in the dependencies it doesnt recognize any step definitions. I tried using load time weaving but I am not sure I totally get how to accomplish both the things.

MagicBeans
  • 343
  • 1
  • 5
  • 18
  • Well I've never seen anybody try to do this before. I really don't know. If you can point to a specific point where it goes wrong people might be able to help you better. – M.P. Korstanje Jan 03 '19 at 16:53
  • The point it goes wrong is that it cannot call the glue codes inside the woven dependency. It says the step is undefined. It cant find methods in it. If I remove the woven dependency in aspectj plugin, I can easily access point cuts inside my project. I want a way to weave the dependency and use its contents. – MagicBeans Jan 04 '19 at 05:07
  • When you say `It says the step is undefined` this means that Cucumber can't find your step. You'll have to work out why this no longer works after weaving. I believe MethodScanner would be a good place to start: https://github.com/cucumber/cucumber-jvm/blob/master/java/src/main/java/cucumber/runtime/java/MethodScanner.java – M.P. Korstanje Jan 04 '19 at 16:06
  • 1
    This setup is way too complex to expect to get an answer here without providing an [MCVE](http://stackoverflow.com/help/mcve) reproducing the problem, ideally on GitHub. If you do provide one, I will take a look. – kriegaex Jan 06 '19 at 03:08

0 Answers0