Aspect not getting called on non-spring maven project

184 Views Asked by At

I'm using Java 8, testng, maven and selenium for UI automation frame work and trying to use aspectj to execute aspects before and after for switching iframes(entry and exit) when any method in an object is called. Aspect never gets called when i run any induvidual test class. Here is my package structure. I tried using both compile time and load time weaving but nothing help.

Project A is the parent The module Project B is inside Project A

Project A pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.projectA</groupId>
        <artifactId>projectA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>
        <name>projectA</name>
        <modules>
        <module>projectB</module>
        </modules>
        <properties>
            <!-- Maven Plugins -->
            <maven-compiler-version>3.6.0</maven-compiler-version>
            <maven-surefire-version>2.20.1</maven-surefire-version>
            <!-- Dependencies -->
            <selenium-version>3.14.0</selenium-version>
            <testng-version>7.4.0</testng-version>
            <log4j2-version>2.17.0</log4j2-version> 
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        </properties>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>${maven-compiler-version}</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                            <useIncrementalCompilation>false</useIncrementalCompilation>
                        </configuration>
                    </plugin>
                    <plugin>
                         <groupId>org.codehaus.mojo</groupId>
                         <artifactId>aspectj-maven-plugin</artifactId>
                         <version>1.8</version>
                         <configuration>
                             <showWeaveInfo>true</showWeaveInfo>
                             <source>1.8</source>
                             <target>1.8</target>
                             <Xlint>ignore</Xlint>
                             <complianceLevel>1.8</complianceLevel>
                             <encoding>UTF-8</encoding>
                             <verbose>true</verbose>
                         </configuration>
                        <executions>
                            <execution>
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                            </execution>
                        </executions>   
                        <dependencies>
                            <dependency>
                                <groupId>org.aspectj</groupId>
                                <artifactId>aspectjtools</artifactId>
                                <version>1.8.13</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${maven-surefire-version}</version>
                        <systemPropertyVariables>
                                <user1>${username}</user1>
                                <!-- Other system variables -->
                            </systemPropertyVariables>
                        <configuration>
                            <argLine>-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.8.13/aspectjweaver-1.8.13.jar</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-java</artifactId>
                    <version>${selenium-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-firefox-driver</artifactId>
                    <version>${selenium-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-htmlunit-driver</artifactId>
                    <version>${htmlunit-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-support</artifactId>
                    <version>${selenium-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.testng</groupId>
                    <artifactId>testng</artifactId>
                    <version>${testng-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
                    <version>${log4j2-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-core</artifactId>
                    <version>${log4j2-version}</version>
                </dependency>
                  <dependency>
                     <groupId>org.aspectj</groupId>
                     <artifactId>aspectjrt</artifactId>
                     <version>1.8.13</version>
                     <scope>runtime</scope>
                 </dependency>
                 <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.13</version>
                <scope>test</scope>
            </dependency>
            </dependencies>
    </project>

Project B pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>projectB</artifactId>
    <name>projectB</name>
    <parent>
        <groupId>com.projectA</groupId>
        <artifactId>projectA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-htmlunit-driver</artifactId>
        </dependency>
        
        <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
        </dependency>
        <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Packaging Structure for Project B.
all the code is in Project B
src/main/java(Holds all the page object classes, interface, aspects and custom annotation classes)
src/test/java(Holds only test classes and has META-INF folder that holds aop.xml)

Interface in src/main/java

 public interface NavBarInterface{
      public void navigateToHelp();
      public void navigateToMenu();
    }

PageObject Class in src/main/java NavigationBar.java

import com.stackoverflow.shop

Class NavigationBar{

   NavigationBar(){}
   @Override
   @HandleFrame
   public void navigateToHelp() {
    // Navigates to Help section
   }

   @Override
   @HandleFrame
   public void navigateToMenu() {
    // Navigates to Help Menu section
   }
}

Custom Annotation class in src/main/java

import com.stackoverflow.shop
        
        @Target(ElementType.METHOD)
        @Retention(RetentionPolicy.RUNTIME)
        public @interface  HandleFrame {
        }

Aspect Class in src/main/java

import com.stackoverflow.shop
    
    @Aspect
    Class FrameHandlingAspect{
    
       @Before("@annotation(com.stackoverflow.shop.NavigationBar)")
        public void enterFrame() {
            logger.info("Switched to IFrame");
            // switches to iframe
        }
    
       @After("@annotation(com.stackoverflow.shop.NavigationBar)")
        public void enterFrame() {
            logger.info("exited from IFrame");
            // exit from iframe
        }
    }

apo.xml file in src/test/java/META-INF

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
 <aspects>
  <aspect name="com.stackoverflow.shop.FrameHandlingAspect"/>
 </aspects> 
 <weaver options="-verbose -debug -showWeaveInfo">
  <include within="com.stackoverflow..*" />
 </weaver>
</aspectj>

TestClass in src/test/java

Class NavigationTests {

@BeforeMethod(alwaysRun = true)
public void refreshAndSwitchTab(){
  // performs refresh page action and opens a new tab
}

@AfterMethod(alwaysRun = true)
public void closeTab(){
  // closes secondary tab
}

@Test(enabled=true)      
public void testNavigation() {
NavBarInterface nbar = new NavigationBar();
nbar.navigateToHelp(); //Aspects are not called before and after
nbar.navigateToMenu();
}
    
}

I'm running the NavigationTests class on Eclipse using Run Configuration/Run as testng test.I also tried running the test as providing vm argument -Dtest=NavigationTests

The tests are running fine but the aspects are never called. Not sure what mistake i'm doing here or does aspect doesn't run when we run a single test class. Any help is much appreciated.

1

There are 1 best solutions below

5
kriegaex On

Your problem seems to be more about Maven basics than AspectJ at all.

Putting AspectJ Maven plugin configuration into your pluginManagement/plugins section without actually activating the plugin in the plugins section, will not help you. So please, make sure to actually use the configured plugin.

<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.14.0</version>  <!-- No reason to use the outdated 1.8 -->
      <configuration>
        <!-- (...) -->
      </configuration>
    <plugin>
  <plugins>
<pluginManagement>

<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <!-- No version/config necessary here, because already configured above) -->
  <plugin>
<plugins>