Using AspectJ to recognize method calls - maven

I like to use AspectJ to recognize when a certain method is called within another OSGi bundle I don't have the source code for. Therefore, I build an aspect-bundle. For learning puposes the other bundle is implemented by a sample bundle which simply implements a "sayHello" method. The aspect-bundle is now supposed to recognize whenever this method is called.
Here is what I have so far:
normal bundle
HelloWorld.java
package com.mango.sample.helloworld;
public class HelloWorld {
public void sayHello() {
System.out.println("Hello world");
}
}
Activator.java
package com.mango.sample.helloworld;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
private static BundleContext context;
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
HelloWorld test = new HelloWorld();
test.sayHello();
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
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.mango.sample</groupId>
<artifactId>helloworld</artifactId>
<version>0.1</version>
<packaging>bundle</packaging>
<name>helloworld</name>
<prerequisites>
<maven>3.0.4</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>*</Import-Package>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.mango.sample.helloworld.Activator</Bundle-Activator>
</instructions>
<manifestLocation>${project.basedir}/META-INF</manifestLocation>
</configuration>
</plugin>
</plugins>
</build>
</project>
aspect-bundle
Activator.java
package com.mango.sample.helloworldj;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
private static BundleContext context;
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
AspectJTest.aj
package com.mango.sample.helloworldj;
public aspect AspectJTest {
before() : execution(* *.sayHello()) {
System.out.println("Before execution sum");
}
}
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.mango.sample</groupId>
<artifactId>helloworldj</artifactId>
<version>0.1</version>
<packaging>bundle</packaging>
<name>helloworldj</name>
<prerequisites>
<maven>3.0.4</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>*</Import-Package>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.mango.sample.helloworldj.Activator</Bundle-Activator>
<Export-Package>com.mango.sample.helloworldj;aspects=AspectJTest</Export-Package>
</instructions>
<manifestLocation>${project.basedir}/META-INF</manifestLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<complianceLevel>1.5</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
After compiling, I start equinox (separately from eclipse)
configuration/config.ini
osgi.framework.extensions=org.eclipse.equinox.weaving.hook_1.0.200.I20130319-1000.jar
osgi.bundles=\
reference\:file\:../lib/org.apache.felix.fileinstall-3.1.6.jar#1:start,\
reference\:file\:../lib/org.eclipse.osgi.services-3.3.100.v20120522-1822#2:start,\
reference\:file\:../lib/org.eclipse.equinox.console-1.0.0.v20120522-1841.jar#start,\
reference\:file\:../lib/slf4j-api-1.7.2.jar#1:start,\
reference\:file\:../lib/logback-classic-1.0.9.jar#1:start,\
reference\:file\:../lib/logback-core-1.0.9.jar#1:start,\
reference\:file\:../lib/org.aspectj.runtime_1.7.3.20130613144500-a.jar#2:start
and the options:
-Daj.weaving.verbose=true
-Dorg.aspectj.weaver.showWeaveInfo=true
-Dorg.aspectj.osgi.verbose=true
Then, I install and start the aspect bundle and the other bundle afterwards. After starting the HelloWorld bundle, I expected something like this
Before execution
Hello world
But I got only Hello world. I guess there is going something wrong with weaving the aspects into the helloworld bundle. Is there something missing in the pom.xml files?
How can I solve it?

Related

Maven/ TestNg can't find allure #step annotation

Having a problem to implement allure #Step annotation into my maven project (tetsng, java).
(Updated) Sharing pom file:
eproject 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>src</groupId>
<artifactId>AutomationTestSuit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src\main\resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src\test\resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<suiteXmlFiles>
<file>${project.basedir}/suites/smoke.xml</file>
</suiteXmlFiles>
<systemPropertyVariables>
<environment.properties>/environment.properties</environment.properties>
</systemPropertyVariables>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.1/aspectjweaver-1.9.1.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>e
I'm trying to add Step annotation to the page class but it gets the failure:
package com.pages.login_page;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class LoginPageElements {
private WebDriver driver;
#FindBy(how = How.ID, using = "test")
private WebElement logonID;
#FindBy(how = How.ID, using = "test")
private WebElement logonPassword;
#FindBy(how = How.ID, using = "test")
private WebElement logonSubmit;
public LoginPageElements(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver,this);
}
#Step("login step") //step is underlined on this place with can't resolve symbol Step error
public void Login_as(String sUsername, String sPassword) {
logonID.sendKeys(sUsername);
logonPassword.sendKeys(sPassword);
logonSubmit.click();
}
#Step error example
the same problem I'm having with #Attachments while implementing it inside of MytestListener class:
see the screenshot
Please help me to figure out the problem.
I noticed that you are using out dated version of Allure. Here is correct import
<!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.6.0</version>
</dependency>
With Allure 2 you do not need to configure Listener, but you need to configure AspectJ instead. Here you can find example for Maven + TestNG: https://docs.qameta.io/allure/#_testng
THE PROBLEM was with the idea community version. It appears that it doesn't support aspectj plugin. If someone will struggle with same issue please tale a look: https://www.jetbrains.com/help/idea/java-compiler.html
I my case the problem was in <scope>test</scope>.
I removed it from dependancy:
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>LAST_VERSION</version>
<scope>test</scope>
</dependency>
and #Step annotation starts to be recognized.

Issue with building pom

I have tried to run example of the project but unsuccessfully (problem with compilation project). Here is the link to this project https://github.com/serenity-bdd/serenity-demos/tree/master/jbehave-webtests
Now I'm trying to make an easy project to learn how to use Serenity with Jbehave but I have problem to create pom.xml. My pom is according to this project https://github.com/serenity-bdd/serenity-demos/tree/master/junit-webtests (I know it is not the best solution). I'm not pretty good in Maven. Here is my pom.xml:
<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>serenity-maven-jbehave</groupId>
<artifactId>imdb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<serenity.version>1.1.25-rc.3</serenity.version>
<serenity.maven.version>1.1.25-rc.3</serenity.maven.version>
<serenity.jbehave.version>1.8.0</serenity.jbehave.version>
<webdriver.driver>firefox</webdriver.driver>
</properties>
<!-- Define the Bintray repos for convenience -->
<repositories>
<repository>
<id>serenity</id>
<name>bintray</name>
<url>http://dl.bintray.com/serenity/maven</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>serenity</id>
<name>bintray-plugins</name>
<url>http://dl.bintray.com/serenity/maven</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>${serenity.version}</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>${serenity.version}</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-rest-assured</artifactId>
<version>${serenity.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.lambdaj</groupId>
<artifactId>lambdaj</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>core</artifactId>
<version>1.0.47</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-jbehave</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<parallel>classes</parallel>
<threadCount>5</threadCount>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18</version>
<configuration>
<includes>
<include>src/main/java/*.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>net.serenity-bdd.maven.plugins</groupId>
<artifactId>serenity-maven-plugin</artifactId>
<version>${serenity.maven.version}</version>
<dependencies>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>1.1.25-rc.3</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>serenity-reports</id>
<phase>post-integration-test</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here is HomePage.
package pages;
import org.openqa.selenium.WebElement;
import net.serenitybdd.core.annotations.findby.By;
import net.serenitybdd.core.annotations.findby.FindBy;
import net.serenitybdd.core.pages.PageObject;
import net.thucydides.core.annotations.DefaultUrl;
#DefaultUrl("https://www.imdb.com")
public class HomePage extends PageObject {
#FindBy(xpath="#navUserMenu.css_nav_menu.js_nav_item")
WebElement downArrowButton;
#FindBy(css="#first_name.reg_thick")
WebElement firstNameInput;
#FindBy(css="#last_name.reg_thick")
WebElement lastNameInput;
#FindBy(css="#year.reg_thick")
WebElement yearOfBirthInput;
#FindBy(css="#gender_m.reg_radio")
WebElement genderMaleRadioButton;
#FindBy(css="#postal.reg_thick")
WebElement zipPostalCodeInput;
#FindBy(css="#email.reg_thick")
WebElement emailInput;
#FindBy(css="#password1.reg_thick")
WebElement passwordInput;
#FindBy(css="#password2.reg_thick")
WebElement passwordConfirmInput;
#FindBy(css="div.reg_right input.btn2.large.primary")
WebElement RegisterButton;
public static String userName;
public void showMoreOptions(){
WebElement downArrow = getDriver().findElement(By.cssSelector("div[id='nb_personal'] span[class='downArrow']"));
downArrow.click();
}
public void clickRegisterButton() {
WebElement register = getDriver().findElement(By.linkText("Register"));
register.click();
}
public void fillRequiredFields(String firstName, String lastName, String email, String password) throws InterruptedException {
firstNameInput.sendKeys(firstName);
lastNameInput.sendKeys(lastName);
String yearOfBirth= String.valueOf(randomWithRange(1950, 1999));
yearOfBirthInput.sendKeys(yearOfBirth);
genderMaleRadioButton.click();
String zipPostalCode = String.valueOf(randomWithRange(10000, 99999));
zipPostalCodeInput.sendKeys(zipPostalCode);
userName= "usermail" + System.currentTimeMillis();
email = userName + "#mailinator.com";
emailInput.sendKeys(email);
passwordInput.sendKeys(password);
passwordConfirmInput.sendKeys(password);
RegisterButton.click();
Thread.sleep(5000);
}
public int randomWithRange(int min, int max) {
int range= (max-min) +1;
return (int) ((Math.random() * range) + min);
}
}
Here is ImdbSteps
package steps;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import net.thucydides.core.annotations.Steps;
import net.thucydides.core.steps.ScenarioSteps;
import pages.HomePage;
public class ImdbSteps extends ScenarioSteps {
private static final long serialVersionUID = 1L;
#Steps
HomePage homePage;
#Given("I am on the imdb site")
public void userWantsVisitSite() {
homePage.open();
}
#When ("I am cliking on register button")
public void userIsClickingRegisterButton() {
homePage.showMoreOptions();
homePage.clickRegisterButton();
}
#Then ("I am filling required fields")
public void userIsFillingRequiredFields(String firstName, String lastName, String email, String password) throws InterruptedException {
homePage.fillRequiredFields(firstName, lastName, email, password);
}
}
Edit:
How should I build pom.xml base on code above? I tried to build this pom.xml with the instruction in link belove
http ://thucydides.info/docs/serenity-staging/#introduction
but I haven't succeed.
Could someone give me a link to the workable project which uses Serenity, Jbehave and Maven? Or any clue how to solve this problem.Thank to in advance.

ContextConfiguration does not seem to load my beans for TestNg testcase

I am new to TestNg. I am trying to test one sample hello world program.
Following is my project structure
Source Package
com.mycompany.mavenproject1 =>HelloWorld.java
Test Package
com.test => NewTestConfig.java & NewTestNGTest.java
Helloworld.java
package com.mycompany.mavenproject1;
public class HelloWorld {
String msg = "test";
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
NewTestConfig.java
package com.test;
import com.mycompany.mavenproject1.HelloWorld;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
public class NewTestConfig {
#Bean
public HelloWorld helloWorldBean() {
HelloWorld hw = new HelloWorld();
return hw;
}
}
NewTestNGTest.java
package com.test;
import com.mycompany.mavenproject1.HelloWorld;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.springframework.test.context.ContextConfiguration;
#Test
#ContextConfiguration(classes={ NewTestConfig.class })
public class NewTestNGTest {
#Autowired
HelloWorld hw ;
#Test
public void exampleOfTestNgMaven() {
String s = hw.getMsg();
System.out.println("This is TestNG-Maven Example" + s);
}
#BeforeClass
public static void setUpClass() throws Exception {
}
#AfterClass
public static void tearDownClass() throws Exception {
}
#BeforeMethod
public void setUpMethod() throws Exception {
}
#AfterMethod
public void tearDownMethod() throws Exception {
}
}
My pom.xml looks like below:
<?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.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>mavenproject1</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
When I try to run the test I get NullpointerException (at hw.getMsg();)
Error stack:
Test set: com.test.NewTestNGTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.209 sec <<< FAILURE!
exampleOfTestNgMaven(com.test.NewTestNGTest) Time elapsed: 0.003 sec <<< FAILURE!
java.lang.NullPointerException
at com.test.NewTestNGTest.exampleOfTestNgMaven(NewTestNGTest.java:25)
I tried to run test in debug mode, I always get HelloWorld bean as null. So eventually test fails due to NullPointerException. I tried many other variations such as instead of config class tried using applicationContext.xml but my bean never gets injected.
For last 2 days I am struggling to make simple test pass but unable to figure out what's going wrong. Why my context is not getting loaded for test?
Any help is greatly appreciated.
Thanks
Manisha

maven plugin descriptor not getting generated

I'm creating a maven plugin, MVN clean install build succeeds but plugin.xml is not getting generated.
#Mojo( name = "cover", defaultPhase = LifecyclePhase.POST_INTEGRATION_TEST)
public class RunCoverage extends AbstractMojo
{
#Parameter( property = "cover.wadl", defaultValue = "test")
private String wadl;
#Parameter( property = "cover.endpoints",defaultValue = "test")
private String endpoints;
#Override
public void execute() throws MojoExecutionException
{
<somecode>
}
}
And the pom.xml is
<?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>
<artifactId>end-point-test-coverage</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<executions>
<execution>
<id>default-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
<phase>process-classes</phase>
</execution>
<execution>
<id>help-descriptor</id>
<goals>
<goal>helpmojo</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Maven clean install doesn't generate plugin.xml
When used in a dependent project, I'm getting the following error
Failed to parse plugin descriptor for it.gruppopam.common:end-point-test-coverage:1 (/home/d/.m2/repository/it/common/end-point-test-coverage/1/end-point-test-coverage-1.jar): No plugin descriptor found at META-INF/maven/plugin.xml -> [Help 1]
[ERROR]
First i would try to set the packaging type to maven-plugin instead of the default which is jar. Furthermore i would suggest to use more up-to-date versions of plugins (maven-compiler-plugin: 3.1) and use a more up-to-date version of maven-plugin-api (3.0? but not 2.0).
you have to remember to change it in your pom.xml to:
<packaging>maven-plugin</packaging>
it is by default:
<packaging>jar</packaging>

Not able to integrate AspectJ with Maven

I banged my head for two days to integrate aspectj with maven, But did not work.
I am writing a simple program with two advices (which works fine in eclipse, so I am sure it's not a code side error).
I have recently started using maven for project building. Can't think of why I am not able to kick off aspectj compiler.
During compilation through maven - I get only class file for java without weaving. .aj file is not compiled.
Please Help!!!!
the first aspect file - ExceptionAspects.aj
package com.aspectSample;
public aspect ExceptionAspects {
pointcut ExceptionHandler(Exception e) : handler(Exception+) && args(e) && within(com.clickable.*);
pointcut callbeforeMethod():execution( public void HelloWorldExclude.*(..)) ;
before(Exception ex) : ExceptionHandler(ex)
{
System.out.println("Added Aspects before Exception :"+ex.toString());
}
before(): callbeforeMethod()
{
System.out.println("Aspect Before Method Call");
}
pointcut sysOutOrErrAccess() : get(* System.out) || get(* System.err);
declare error
: sysOutOrErrAccess()
: "Don't write to the console";
}
The source java file HelloWorldExclude.java
package com.aspectSample;
import java.sql.SQLException;
public class HelloWorldExclude {
private void FoulIntro(String message, String name) throws SQLException
{
throw new SQLException("WriteSomething");
}
public void GiveMeFoulIntro(String message, String name)
{
try
{
this.FoulIntro(message, name);
}catch(SQLException exp)
{
System.out.println("catching exception");
}
}
}
and the pom.xml file is
<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.clickable.HelloWorld-Maven</groupId>
<artifactId>HelloWorldAspect-Maven</artifactId>
<version>1.0.0</version>
<name>HelloWorld Aspect</name>
<packaging>jar</packaging>
<description>Hello World Code</description>
<properties>
<java-version>1.6</java-version>
<org.aspectj-version>1.6.11</org.aspectj-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
</dependencies>
</project>
A couple of things that you can try:
Explicitly provide the dependencies of the maven-aspectj plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<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>
with the version of aspectjrt and aspectjtools matching the version of aspectj that you are using.
Do you by any chance have any of your sources in the test folder,
with the src/test folder, then to weave the aspects to the test sources you will have to explicitly mention that in the maven-aspectj plugin:
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
There was a stupid mistake in pom file.
aspectj plugin was added under element, which is just a configuration of all plugins. It doesn't tell compiler what to use.
It worked as soon as I removed plugin out of this element.
Thanks
Pankaj

Resources