TestNG's #Test annotation not executed properly with the mvn command - java

Everything with the #Test annotation throws null pointer exception, regardless of the method called. I tried calling driver.findElement() and executing js script with JavascriptExecutor. Both methods ended up with a null pointer.
If I run through the IDE everything works like a charm. Otherwise, only the Before and After annotations are executed adequately
Here's my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<parallel>methods</parallel>
<threadCount>1</threadCount>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache-extras.beanshell</groupId>
<artifactId>bsh</artifactId>
<version>2.0b6</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
and testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Practice Suite">
<test name="Test Basics 1">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[
String testGroup = System.getProperty("env");
groups.containsKey(testGroup);
]]>
</script>
</method-selector>
</method-selectors>
<classes>
<class name="tests.SignUpTest"/>
</classes>
</test>
</suite>
and finally the two manners I am using to run the test:
mvn -Dwebdriver=chrome -Denv=qa install
mvn -Dwebdriver=chrome -Denv=qa test
EDIT:
BeforeTest and Test:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
public class BaseTest {
protected WebDriver driver;
private WebDriver selectDriver(String driver)
switch (driverString) {
case "chrome":
return new ChromeDriver();
case "firefox":
return new FirefoxDriver();
case "edge":
return new EdgeDriver();
case "safari":
return new SafariDriver();
default:
return new ChromeDriver();
}
}
#BeforeTest(alwaysRun = true)
public void testInit() {
driver = selectDriver(System.getProperty("webdriver"));
}
}
import additions.BaseTest;
import org.testng.Assert;
import org.testng.annotations.Test;
import pages.DashBoardPage;
import pages.LoginPage;
public class LoginTest extends BaseTest {
#Test
public void successfulLogin(){
LoginPage loginPage = new LoginPage(driver);
loginPage
.populateUserName("somename")
.populatePassword("somepassowrd")
.clickLoginButton();
}
}

Since still there is some uncertainty in the question, I'll try to share my thoughts around this.
If I run through the IDE everything works like a charm.
There are several ways how to run this in IDE:
Run the test class.
In this case IDE generates own testng.xml which not match the example (from the question), so it will not contain any method-selector items.
Run the testng.xml
In testng.xml there is method-selector which expects System.getProperty("env") provided.
If you run testng.xml from IDE, you have to provide somehow
System.getProperty("env") via IDE settings, otherwise you'll see org.testng.TestNGException: javax.script.ScriptException error.
If I look at testng.xml, I see method-selector, which filter tests by System.getProperty("env").
And If I look at the test:
#Test
public void successfulLogin(){
LoginPage loginPage = new LoginPage(driver);
I see no any group defined for it. So this test cannot be executed with maven command. And it's unclear how it might throw any exception.
If I look at maven command
mvn -Dwebdriver=chrome -Denv=qa test
I cannot see the arg defining which testng xml suite to execute.
I expect the command like:
mvn test -Dwebdriver=chrome -Denv=qa -Dsurefire.suiteXmlFiles="src/test/resources/tests.xml"
Expected behavior
When we execute the maven command:
mvn test -Dwebdriver=chrome -Denv=qa -Dsurefire.suiteXmlFiles="src/test/resources/tests.xml"
method-selector in testng.xml looks for methods matching the qa group.
and there are no any methods, matched this group.
But there are some configuration methods with alwaysRun = true, and they will be executed anyway.
So
#BeforeTest(alwaysRun = true)
public void testInit() {
is invoked without any tests,
and the output will be:
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.345 s - in TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.867 s
And the test will be executed if we'll add the group:
#Test(groups= {"qa"})
public void successfulLogin(){

I've solved it. It turns out that initialising the driver with the #BeforeTest annotation is not a good idea, especially if your teardown method is marked with #AfterTest. The driver is initialised and used correctly in the first #Test method, but on the second one, the teardown method is already called and the driver becomes null. So I used #BeforeClass and #AfterClass (I might reconsider and use #Before/AfterSuite). Then I added the following configuration to the pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
Then I added the package with the tests I wanted to execute, in the testng.xml file, like this:
<packages>
<package name="path.to.tests.*"></package>
</packages>
Finally I called the mvn command using the following syntax:
mvn test -Dsurefire.suiteXmlFiles=testng.xml -Dwebdriver="chrome" -Denv="qa"
This way you don't have to call the path to the testng.xml file, but rather call it out of the pom.xml. As a side note to the last sentence, my testng.xml file is located in the project directory, along with the src folder. If you put it in another folder, I guess it will be good to set the path to this folder in the pom.xml
I hope this is understandable and it will be helpful for anyone who gets stuck with such issue.
Thank again to the others for the clues.

Related

groovy-eclipse-compiler compiles but javac compilation fails

My project builds successfully with groovy-eclipse-compiler, but fails without groovy-eclipse-compiler (using just javac). The build fails with an error message as given below (reported in a test class, while mocking an invocation)
java: reference to getFileResource is ambiguous
In order to debug the issue, I created a project with minimal files (given below). Though in project we have groovy source also, but I have not included them here to keep the code minimal.
The code is also pushed to git and is available at https://github.com/kaushalkumar/project-debug
My Doubt: The reported issue looks to be legitimate and I feel that groovy-eclipse-compiler must also fail, but it seems that the error is ignored. I am trying to understand what make groovy compiler to ignore it. Is it an issue in groovy compiler?
src/main/java/pkg1/IStrategy.java
package pkg1;
import java.util.Map;
public interface IStrategy {
Map<String, Object> getEnvMap();
}
src/main/java/pkg1/SharedResourceHelper.java
package pkg1;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class SharedResourceHelper {
public static File getFileResource(final String resourceName, final IStrategy strategy) throws IOException {
return getFileResource(resourceName, strategy.getEnvMap());
}
public static File getFileResource(final String resourceName, final Map<String, Object> envConfig) throws IOException {
return null;
}
}
src/test/java/pkg1/StrategyTest.java
package pkg1;
import pkg1.SharedResourceHelper;
import org.easymock.EasyMock;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.junit.Test;
import org.powermock.modules.junit4.PowerMockRunner;
import org.junit.runner.RunWith;
import java.io.File;
#PrepareForTest({SharedResourceHelper.class})
#RunWith(PowerMockRunner.class)
public class StrategyTest {
#Test
#PrepareForTest({SharedResourceHelper.class})
public void testGetFileResource() throws Exception {
PowerMock.mockStatic(SharedResourceHelper.class);
EasyMock.expect(SharedResourceHelper.getFileResource(EasyMock.anyString(), EasyMock.anyObject())).andReturn(File.createTempFile("tmp", "s"));
// EasyMock.expect(SharedResourceHelper.getFileResource("test", null)).andReturn(File.createTempFile("tmp", "s"));
}
}
/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>project.debug</groupId>
<artifactId>project</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<source>1.8</source>
<target>1.8</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.9.2-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.4.3-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Java version - 1.8.0_231
Maven - 3.6.2
OS - Mac 10.15.6
groovy-eclipse-compiler - 2.9.2-01
groovy-eclipse-batch - 2.4.3-01
You reference "SharedResourceHelper.getFileResource(EasyMock.anyString(), EasyMock.anyObject())" is indeed ambiguous. If you add a typecast before "EasyMock.anyObject()" you could disambiguate. And EasyMock probably provides an "any" method that you can pass a type into as well.
groovy-eclipse-compiler is based upon ecj (eclipse compiler for java) and not javac, so there are bound to be differences. It may also be that ecj has a different default error/warning level for this particular case. If you feel this should be an error, you can file a JDT bug at bugs.eclipse.org.
eric-milles gave some direction to further explore this. His input is available at https://github.com/groovy/groovy-eclipse/issues/1157.
Based on his comment, we explored the history of https://github.com/groovy/groovy-eclipse/blob/master/extras/groovy-eclipse-batch-builder/build.properties and found that the compilation issue was between 2.4.12-01 (compilation works) and 2.4.12-02 (compilation breaks- as expected), which was part of release 2.9.2.
The change happened on Aug 10, 2017 (13c1c2a#diff-c8c111c3afb6080ae6b32148caaf6a0a), with comment as "Remove codehaus references". The jdt.patch.target was targeted for e44 which is Luna. This was same for both the files.
I invested some time in exploring https://github.com/eclipse/eclipse.jdt.core, to figure out how compiler behaviour could have altered, but could not get much. Though I am not very sure, but I feel that change in groovy-eclipse-batch (between 2.4.12-01 and 2.4.12-02) might be the cause of this.
Having invested this much time, I feel that it is not worth to further debug on this to figure out the root cause as the issue is already fixed in next version(s) [2.4.12-02 and beyond].

TestNG - How to print run time testng parameters in testng custom emailable report?

I found there is an option to set the parameters to testng xml through surefire plugin, by then the parameter can be sent from command line.
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemPropertyVariables>
<browser>firefox</browser>
</systemPropertyVariables>
</configuration>
</plugin>
[...]
</plugins>
Ref:
https://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html
https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html
There is a requirement to print the parameters in testng custom emailable report. Able to print the testng parameters specified in testng xml using the following code. But, not able to print the parameters specified in surefire plugin.
Note: System.getProperty("browser") works here. But, I want to print them without having to specifying the parameter name (say "browser") as below. But below one doesn't work.
Map<String, String> allParameters = context.getCurrentXmlTest().getAllParameters();
for(String parameter: allParameters.keySet()) {
System.out.println(parameter + " : " + allParameters.get(parameter));
}
Example:
import java.util.Map;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestNGTest {
ITestContext context;
#BeforeTest
public void beforeTest(ITestContext context) {
this.context = context;
}
#Parameters({"browser"})
#Test
public void method(String browser) {
System.out.println(browser);
Map<String, String> allParameters = context.getCurrentXmlTest().getAllParameters();
for(String parameter: allParameters.keySet()) {
System.out.println(parameter + " : " + allParameters.get(parameter));
}
}
}
Actual Output:
[RemoteTestNG] detected TestNG version 7.0.0
chrome
key : value
===============================================
Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Expected Output:
[RemoteTestNG] detected TestNG version 7.0.0
chrome
key : value
browser : chrome
===============================================
Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" thread-count="4">
<test name="Front-End" group-by-instances="true">
<parameter name="key" value="value"></parameter>
<classes>
<class name="com.ftd.automation.framework.tests.TestNGTest" />
</classes>
</test>
</suite>
Please help me on how to print the testng parameters specified in surefire plugin or passed in command line.
Assuming you are running with command line arguments like,
mvn test -Dbrowser=firefox
then get the parameter by,
import org.testng.annotations.Parameters;
#Parameters("browser")
#Test
public void myTestMethod(String browser){
System.out.println(browser);
}
//or as Test parameter
#Test(parameters = {"browser"})
public void myTestMethod(String browser){
System.out.println(browser);
}
//or System.getProperty() way
#Test
public void myTestMethod(){
System.out.println(System.getProperty("browser"));
}
The above works well. Additionally, if you need to use testng.xml, you can specify the suiteXmlFile like,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemPropertyVariables>
<browser>firefox</browser>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
EDIT:
FYI, System.grtProperties() lists all properties, and those set from command line will be there, but there's no way to distinguish those from the other properties added by the system

org.testng.TestNGException: Cannot instantiate cucumber test runner

hi guys im trying to run my cucumber framework using testng, im pretty sure im a version issue but id appreciate it if someone can point the problem here thanks
features:
Feature: Application Login
Scenario: Home page default login
Given user is on landing page
When user logins into the application with username "jon" and password
"1234"
Then Home page is displayed
And Cards displayed "true"
Scenario: Home page default login2
Given user is on landing page
When user logins into the application with username "john" and password
"4321"
Then Home page is displayed
And Cards displayed "false"
Definitions:
package StepDefinations;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.And;
public class LoginDefination {
#Given("^user is on landing page$")
public void user_is_on_landing_page() throws Throwable {
//code
System.out.println("on landing page");
}
#When("^user logins into the application with username and password$")
public void user_logins_into_the_application_with_username_and_password() throws Throwable {
//code
System.out.println("logging in");
}
#When("^user logins into the application with username \"([^\"]*)\" and password \"([^\"]*)\"$")
public void user_logins_into_the_application_with_something_and_password_something(String strArg1, String strArg2) throws Throwable {
System.out.println(strArg1 +" "+ strArg2);
}
#Then("^Home page is displayed$")
public void home_page_is_displayed() throws Throwable {
//code
System.out.println("hello homepage");
}
#And("^Cards are displayed$")
public void cards_are_displayed() throws Throwable {
//code
System.out.println("hello cards");
}
#And("^Cards are not displayed$")
public void cards_are_not_displayed() throws Throwable {
//code
System.out.println("hello not cards");
}
#And("^Cards displayed \"([^\"]*)\"$")
public void cards_displayed(String args) throws Throwable {
//code
System.out.println("this will or will not display the cards - " + args);
}
}
test runner file:
package CucumberOptions;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
//Feature
#CucumberOptions(
features = "src\\test\\java\\features",
//if u want tp execute everything then just give the path till the package level
glue= "StepDefinations"//Package name
)
public class TestRunner extends AbstractTestNGCucumberTests {
}
testng xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="cucumber testing 101">
<classes>
<class name="CucumberOptions.testme"/>
<class name="CucumberOptions.TestRunner"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
maven pom.xml file:
<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>Cucumber</groupId>
<artifactId>Automation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Automation</name>
<url>http://maven.apache.org</url>
<build>
<resources>
<resource>
<directory>src/main/java/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
error:
[RemoteTestNG] detected TestNG version 6.10.0
org.testng.TestNGException:
Cannot instantiate class CucumberOptions.TestRunner
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:363)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:275)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
at org.testng.TestClass.getInstances(TestClass.java:100)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:86)
at org.testng.TestClass.init(TestClass.java:78)
at org.testng.TestClass.<init>(TestClass.java:41)
at org.testng.TestRunner.initMethods(TestRunner.java:425)
at org.testng.TestRunner.init(TestRunner.java:252)
at org.testng.TestRunner.init(TestRunner.java:222)
at org.testng.TestRunner.<init>(TestRunner.java:171)
at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:623)
at org.testng.SuiteRunner.init(SuiteRunner.java:189)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:136)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1375)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1355)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 25 more
Caused by: java.lang.NoSuchMethodError: cucumber.runtime.RuntimeOptionsFactory.<init>(Ljava/lang/Class;[Ljava/lang/Class;)V
at cucumber.api.testng.AbstractTestNGCucumberTests.<init>(AbstractTestNGCucumberTests.java:27)
at CucumberOptions.TestRunner.<init>(TestRunner.java:14)
... 30 more
its a simple piece of code but im just trying to make it work using testng then maven, if someone can help me out i will be very thankful :)
Incompatible versions of TestNG and Cucumber libraries. Validate the versions of them in your dependency tree and check what version of TestNG fits to the version of your Cucumber library, for example here:
https://mvnrepository.com/artifact/info.cukes/cucumber-testng
https://mvnrepository.com/artifact/io.cucumber/cucumber-testng
UPDATE: I noticed that you have actually posted your pom.xml
Update the version of cucumber-testng to 1.2.5 (use the same version as for cucumber-java). Use testng version 6.9.10 (if you do not mind). This way you will align all libraries versions.
CONFIRMED: You have a conflict between cucumber-testng ver. 1.1.5 and cucumber-java ver. 1.2.5. Set both libraries to either version 1.1.5, or update them to use version 1.2.5.

Hadoop 2.7.1 on Ubuntu 14.04 and Eclipse Local Run: java.lang.NoClassDefFoundError

I recently started learning how to setup Hadoop and have written my first application by following Hadoop: The Definitive Guide book. However, I am stuck on a step where I have to run my application locally on a small piece of data.
mvn compile
export HADOOP_CLASSPATH=~/workspace/hadoop-book-mr-dev/target/classes/com/hadoopbook/hadoop_book_mr_dev/
hadoop MaxTemperatureDriver -conf conf/hadoop-local.xml input/ncdc/micro output
I ran the commands above in the command line and exported the Eclipse workspace which contains the required classes as my $HADOOP_CLASSPATH. The last command which uses my application to analyze the data does not succeed and gives me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: MaxTemperatureDriver (wrong name: com/hadoopbook/hadoop_book_mr_dev/MaxTemperatureDriver)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
I have tried different folders and directories as my classpath but to no avail. Additionally, I read other similar topics and did Clean->Clean Selected Projects as well as removing the source folder from my buildpath and then add it back again but none of them are working for me.
Finally, I have also checked that I have commons-logging-1.1.1.jar and hadoop-yarn-common-2.7.1.jar as buildpath in my Eclipse. I am not sure what else to try at the moment and would appreciate it if anybody could guide me through this step by step.
Here are some additional information to help in debugging the problem. The pom.xml that the mvn eclipse step builds from:
<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.hadoopbook</groupId>
<artifactId>hadoop-book-mr-dev</artifactId>
<version>4.0</version>
<name>hadoop-book-mr-dev</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hadoop.version>2.7.1</hadoop.version>
</properties>
<dependencies>
<!-- Hadoop main client artifact -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<!-- Unit test artifact -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.mrunit</groupId>
<artifactId>mrunit</artifactId>
<version>1.1.0</version>
<classifier>hadoop2</classifier>
<scope>test</scope>
</dependency>
<!-- Hadoop test artifact for running mini clusters -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-minicluster</artifactId>
<version>${hadoop.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>hadoop-examples</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugins</artifactId>
<version>2.5</version>
<configuration>
<outputDirectory>${basedir}</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
The Driver class that the hadoop command tries to run:
package com.hadoopbook.hadoop_book_mr_dev;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class MaxTemperatureDriver extends Configured implements Tool {
#Override
public int run(String[] args) throws Exception {
if(args.length != 2){
System.err.printf("Usage: $s [generic options] <input> <output> \n",
getClass().getSimpleName());
ToolRunner.printGenericCommandUsage(System.err);
return -1;
}
Job job = new Job(getConf(), "Max temperature");
job.setJarByClass(getClass());
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(MaxTemperatureMapper.class);
job.setCombinerClass(MaxTemperatureReducer.class);
job.setReducerClass(MaxTemperatureReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new MaxTemperatureDriver(), args);
System.exit(exitCode);
}
}
The list of JARs and Maven Dependencies in my build path:
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-client/2.7.1/hadoop-client-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-common/2.7.1/hadoop-common-2.7.1.jar
/home/ubuntu/.m2/repository/commons-cli/commons-cli/1.2/commons-cli-1.2.jar
/home/ubuntu/.m2/repository/org/apache/commons/commons-math3/3.1.1/commons-math3-3.1.1.jar
/home/ubuntu/.m2/repository/xmlenc/xmlenc/0.52/xmlenc-0.52.jar
/home/ubuntu/.m2/repository/commons-httpclient/commons-httpclient/3.1/commons-httpclient-3.1.jar
/home/ubuntu/.m2/repository/commons-codec/commons-codec/1.4/commons-codec-1.4.jar
/home/ubuntu/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar
/home/ubuntu/.m2/repository/commons-net/commons-net/3.1/commons-net-3.1.jar
/home/ubuntu/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar
/home/ubuntu/.m2/repository/javax/servlet/jsp/jsp-api/2.1/jsp-api-2.1.jar
/home/ubuntu/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar
/home/ubuntu/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar
/home/ubuntu/.m2/repository/commons-configuration/commons-configuration/1.6/commons-configuration-1.6.jar
/home/ubuntu/.m2/repository/commons-digester/commons-digester/1.8/commons-digester-1.8.jar
/home/ubuntu/.m2/repository/commons-beanutils/commons-beanutils/1.7.0/commons-beanutils-1.7.0.jar
/home/ubuntu/.m2/repository/commons-beanutils/commons-beanutils-core/1.8.0/commons-beanutils-core-1.8.0.jar
/home/ubuntu/.m2/repository/org/slf4j/slf4j-api/1.7.10/slf4j-api-1.7.10.jar
/home/ubuntu/.m2/repository/org/slf4j/slf4j-log4j12/1.7.10/slf4j-log4j12-1.7.10.jar
/home/ubuntu/.m2/repository/org/codehaus/jackson/jackson-core-asl/1.9.13/jackson-core-asl-1.9.13.jar
/home/ubuntu/.m2/repository/org/codehaus/jackson/jackson-mapper-asl/1.9.13/jackson-mapper-asl-1.9.13.jar
/home/ubuntu/.m2/repository/org/apache/avro/avro/1.7.4/avro-1.7.4.jar
/home/ubuntu/.m2/repository/com/thoughtworks/paranamer/paranamer/2.3/paranamer-2.3.jar
/home/ubuntu/.m2/repository/org/xerial/snappy/snappy-java/1.0.4.1/snappy-java-1.0.4.1.jar
/home/ubuntu/.m2/repository/com/google/protobuf/protobuf-java/2.5.0/protobuf-java-2.5.0.jar
/home/ubuntu/.m2/repository/com/google/code/gson/gson/2.2.4/gson-2.2.4.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-auth/2.7.1/hadoop-auth-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/httpcomponents/httpclient/4.2.5/httpclient-4.2.5.jar
/home/ubuntu/.m2/repository/org/apache/directory/server/apacheds-kerberos-codec/2.0.0-M15/apacheds-kerberos-codec-2.0.0-M15.jar
/home/ubuntu/.m2/repository/org/apache/directory/server/apacheds-i18n/2.0.0-M15/apacheds-i18n-2.0.0-M15.jar
/home/ubuntu/.m2/repository/org/apache/directory/api/api-asn1-api/1.0.0-M20/api-asn1-api-1.0.0-M20.jar
/home/ubuntu/.m2/repository/org/apache/directory/api/api-util/1.0.0-M20/api-util-1.0.0-M20.jar
/home/ubuntu/.m2/repository/org/apache/curator/curator-framework/2.7.1/curator-framework-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/curator/curator-client/2.7.1/curator-client-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/curator/curator-recipes/2.7.1/curator-recipes-2.7.1.jar
/home/ubuntu/.m2/repository/com/google/code/findbugs/jsr305/3.0.0/jsr305-3.0.0.jar
/home/ubuntu/.m2/repository/org/apache/htrace/htrace-core/3.1.0-incubating/htrace-core-3.1.0-incubating.jar
/home/ubuntu/.m2/repository/org/apache/zookeeper/zookeeper/3.4.6/zookeeper-3.4.6.jar
/home/ubuntu/.m2/repository/org/apache/commons/commons-compress/1.4.1/commons-compress-1.4.1.jar
/home/ubuntu/.m2/repository/org/tukaani/xz/1.0/xz-1.0.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-hdfs/2.7.1/hadoop-hdfs-2.7.1.jar
/home/ubuntu/.m2/repository/org/mortbay/jetty/jetty-util/6.1.26/jetty-util-6.1.26.jar
/home/ubuntu/.m2/repository/io/netty/netty-all/4.0.23.Final/netty-all-4.0.23.Final.jar
/home/ubuntu/.m2/repository/xerces/xercesImpl/2.9.1/xercesImpl-2.9.1.jar
/home/ubuntu/.m2/repository/xml-apis/xml-apis/1.3.04/xml-apis-1.3.04.jar
/home/ubuntu/.m2/repository/org/fusesource/leveldbjni/leveldbjni-all/1.8/leveldbjni-all-1.8.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-mapreduce-client-app/2.7.1/hadoop-mapreduce-client-app-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-mapreduce-client-common/2.7.1/hadoop-mapreduce-client-common-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-yarn-client/2.7.1/hadoop-yarn-client-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-mapreduce-client-shuffle/2.7.1/hadoop-mapreduce-client-shuffle-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-yarn-api/2.7.1/hadoop-yarn-api-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-mapreduce-client-core/2.7.1/hadoop-mapreduce-client-core-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-yarn-common/2.7.1/hadoop-yarn-common-2.7.1.jar
/home/ubuntu/.m2/repository/javax/xml/bind/jaxb-api/2.2.2/jaxb-api-2.2.2.jar
/home/ubuntu/.m2/repository/javax/xml/stream/stax-api/1.0-2/stax-api-1.0-2.jar
/home/ubuntu/.m2/repository/javax/activation/activation/1.1/activation-1.1.jar
/home/ubuntu/.m2/repository/com/sun/jersey/jersey-client/1.9/jersey-client-1.9.jar
/home/ubuntu/.m2/repository/org/codehaus/jackson/jackson-jaxrs/1.9.13/jackson-jaxrs-1.9.13.jar
/home/ubuntu/.m2/repository/org/codehaus/jackson/jackson-xc/1.9.13/jackson-xc-1.9.13.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-mapreduce-client-jobclient/2.7.1/hadoop-mapreduce-client-jobclient-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-annotations/2.7.1/hadoop-annotations-2.7.1.jar
/home/ubuntu/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
/home/ubuntu/.m2/repository/org/apache/mrunit/mrunit/1.1.0/mrunit-1.1.0-hadoop2.jar
/home/ubuntu/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
/home/ubuntu/.m2/repository/org/mockito/mockito-core/1.9.5/mockito-core-1.9.5.jar
/home/ubuntu/.m2/repository/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1.jar
/home/ubuntu/.m2/repository/org/objenesis/objenesis/1.0/objenesis-1.0.jar
/home/ubuntu/.m2/repository/com/google/guava/guava/11.0.2/guava-11.0.2.jar
/home/ubuntu/.m2/repository/org/powermock/powermock-core/1.5.1/powermock-core-1.5.1.jar
/home/ubuntu/.m2/repository/org/powermock/powermock-reflect/1.5.1/powermock-reflect-1.5.1.jar
/home/ubuntu/.m2/repository/org/javassist/javassist/3.18.0-GA/javassist-3.18.0-GA.jar
/home/ubuntu/.m2/repository/org/powermock/powermock-api-mockito/1.5.1/powermock-api-mockito-1.5.1.jar
/home/ubuntu/.m2/repository/org/powermock/powermock-api-support/1.5.1/powermock-api-support-1.5.1.jar
/home/ubuntu/.m2/repository/org/powermock/powermock-module-junit4/1.5.1/powermock-module-junit4-1.5.1.jar
/home/ubuntu/.m2/repository/org/powermock/powermock-module-junit4-common/1.5.1/powermock-module-junit4-common-1.5.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-minicluster/2.7.1/hadoop-minicluster-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-common/2.7.1/hadoop-common-2.7.1-tests.jar
/home/ubuntu/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar
/home/ubuntu/.m2/repository/org/mortbay/jetty/jetty/6.1.26/jetty-6.1.26.jar
/home/ubuntu/.m2/repository/com/sun/jersey/jersey-core/1.9/jersey-core-1.9.jar
/home/ubuntu/.m2/repository/com/sun/jersey/jersey-json/1.9/jersey-json-1.9.jar
/home/ubuntu/.m2/repository/org/codehaus/jettison/jettison/1.1/jettison-1.1.jar
/home/ubuntu/.m2/repository/com/sun/xml/bind/jaxb-impl/2.2.3-1/jaxb-impl-2.2.3-1.jar
/home/ubuntu/.m2/repository/com/sun/jersey/jersey-server/1.9/jersey-server-1.9.jar
/home/ubuntu/.m2/repository/asm/asm/3.1/asm-3.1.jar
/home/ubuntu/.m2/repository/net/java/dev/jets3t/jets3t/0.9.0/jets3t-0.9.0.jar
/home/ubuntu/.m2/repository/org/apache/httpcomponents/httpcore/4.1.2/httpcore-4.1.2.jar
/home/ubuntu/.m2/repository/com/jamesmurty/utils/java-xmlbuilder/0.4/java-xmlbuilder-0.4.jar
/home/ubuntu/.m2/repository/com/jcraft/jsch/0.1.42/jsch-0.1.42.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-hdfs/2.7.1/hadoop-hdfs-2.7.1-tests.jar
/home/ubuntu/.m2/repository/commons-daemon/commons-daemon/1.0.13/commons-daemon-1.0.13.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-yarn-server-tests/2.7.1/hadoop-yarn-server-tests-2.7.1-tests.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-yarn-server-common/2.7.1/hadoop-yarn-server-common-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-yarn-server-nodemanager/2.7.1/hadoop-yarn-server-nodemanager-2.7.1.jar
/home/ubuntu/.m2/repository/com/google/inject/guice/3.0/guice-3.0.jar
/home/ubuntu/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar
/home/ubuntu/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar
/home/ubuntu/.m2/repository/com/sun/jersey/contribs/jersey-guice/1.9/jersey-guice-1.9.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-yarn-server-resourcemanager/2.7.1/hadoop-yarn-server-resourcemanager-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-yarn-server-applicationhistoryservice/2.7.1/hadoop-yarn-server-applicationhistoryservice-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-yarn-server-web-proxy/2.7.1/hadoop-yarn-server-web-proxy-2.7.1.jar
/home/ubuntu/.m2/repository/org/apache/zookeeper/zookeeper/3.4.6/zookeeper-3.4.6-tests.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-mapreduce-client-jobclient/2.7.1/hadoop-mapreduce-client-jobclient-2.7.1-tests.jar
/home/ubuntu/.m2/repository/com/google/inject/extensions/guice-servlet/3.0/guice-servlet-3.0.jar
/home/ubuntu/.m2/repository/io/netty/netty/3.6.2.Final/netty-3.6.2.Final.jar
/home/ubuntu/.m2/repository/org/apache/hadoop/hadoop-mapreduce-client-hs/2.7.1/hadoop-mapreduce-client-hs-2.7.1.jar
Thanks in advance!

How to run maven tests in a specific order? [duplicate]

This question already has answers here:
How do I control the order of execution of tests in Maven?
(6 answers)
Closed 7 years ago.
I have a maven project and several test classes.
I want to run these tests in a specific order with the plug-in surefire.
For example, I have:
ClassTest1.java
ClassTest2.java
ClassTest3.java
ClassTest4.java
I want to run the Class 1, then 2, then 3 and finally 4.
How can I specify this in the pom.xml?
One workaround is to set the runOrder parameter to alphabetical and then rename your tests to have alphabetical order.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<runOrder>alphabetical</runOrder>
</configuration>
</plugin>
This isn't recommended, though - unit tests should be independent of each other. The execution order shouldn't matter.
You can do this with the runOrder parameter.
From the documentation:
Defines the order the tests will be run in. Supported values are
"alphabetical", "reversealphabetical", "random", "hourly"
(alphabetical on even hours, reverse alphabetical on odd hours),
"failedfirst", "balanced" and "filesystem".
See the full description here.
Here is an example
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<runOrder>alphabetical</runOrder>
</configuration>
</plugin>
I would suggest you to define the order in testng.xml and then create profile via maven and run it. Here is a code sample you can try out:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Selenium Automation" parallel="false">
<test name="Test1">
<classes>
<class name="some.package.Class1"/>
<class name="some.package.Class2"/>
<class name="some.package.Class3"/>
</classes>
</test>
</suite>
And then in POM.xml you can create a profile as below and refer to the testNG.xml which you want to execute.
<profile>
<id>any id</id>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin/>
</profile>
You could use a JUnitCore to execute the tests in a specific order. Here is a basic implementation.
public static void main(String[] args)
{
List<Class> testCases = new ArrayList<Class>();
//Add test cases
testCases.add(Class1.class);
testCases.add(Class2.class);
for (Class testCase : testCases)
{
runTestCase(testCase);
}
}
private static void runTestCase(Class testCase)
{
Result result = JUnitCore.runClasses(testCase);
for (Failure failure : result.getFailures())
{
System.out.println(failure.toString());
}
}
Is there a reason you need them executed in a specific order? The tests should ideally not have dependencies on each other.
You can create a testsuite class with junit and then use surefire to run this class instead, it will run with the order you give, for example:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
#RunWith(Suite.class)
#SuiteClasses({
Test1.class
Test2.class
Test3.class
Test4.class
})
public class TestSuite {
}

Categories