cucumber.runtime.cucumberException: Failed to instantiate class CucumberTestDefinition.CucumberStepDefinition - java

I have gone through all previous StackOverflow issues related to the same. This post will be long so please bear with me. The folders in my cucumber project are ordered as follows:
-src/main/java
-src/main/resources
-src/test/java
-|CucumberRunner (package)
-|CucumberTestRunner.java
-|CucumberTestDefinition (package)
-|CucumberStepDefinition.java
-src/test/resources
-CucumberFeaturesFolder
-|CucumberFeatureFile.feature
Here is a picture of the arrangement of the Project folders if the above order did not make sense to you. Order of project folders inside the project
My pom.xml has the following dependency added (no more dependency):
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
My CucumberTestRunner.java file contains the following:
package CucumberRunner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "CucumberFeaturesFolder/CucumberFeatureFile.feature",
glue = {"src/test.java/CucumberTestDefinition"}
)
public class CucumberTestRunner {}
The error that I get when I try to run CucumberFeatureFile.feature is the following:
cucumber.runtime.CucumberException: Failed to instantiate class CucumberTestDefinition.CucumberStepDefinition
Now, after reading the similar posts mentioned on StackOverflow, I tried changing the version of cucumber-unit & cucumber-java from 1.2.2 to 1.2.0 which also resulted in an error but a different one:
Exception in thread “main” cucumber.runtime.CucumberException: No backends were found

Change
glue = {"src/test.java/CucumberTestDefinition"}
to
glue = {"src/test/java/CucumberTestDefinition"}

Make sure the Java version is compatible with cucumber dependencies and other dependencies added in POM.xml. Earlier I was tried with JDK 7 but after changing to JDK 8, the error/exception was no longer visible at runtime and was able to execute the test successfully.

Restart the Appium Server and run again
Also check the all saved locator. If you create any Mobile/Web element and don't assign any value you can face CucumberException.
#iOSXCUITFindBy(accessibility = "")ERROR REASON
public MobileElement submitButton;
#iOSXCUITFindBy(accessibility = "submit") TRUE
public MobileElement submitButton;

Related

Why do I receive fails when I try to run my framework? [duplicate]

Im having issues running a feature in Cucumber, the feature is very basic as it's from a tutorial.
It is not defined and is as follows:
Feature: Proof that my concept works
Scenario: My first test
Given this is my first step
When this is my second step
Then this is my final step
My Cucumber runner class is as follows:
package cucumber;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#Cucumber.Options(
format = {"pretty", "json:target/"},
features = {"src/cucumber/"}
)
public class CucumberRunner {
}
Also the external .jar files that I have in the project are as follows:
The exception that I'm getting is:
Exception in thread "main" cucumber.runtime.CucumberException: Failed
to instantiate public
cucumber.runtime.java.JavaBackend(cucumber.runtime.io.ResourceLoader)
with [cucumber.runtime.io.MultiLoader#75d837b6]
I've tried to look around online for the solution to this problem but have not had any luck.
I've also discussed with the OP of the tutorial and I'm still awaiting feedback but it has been a while.
I ran into a similar issue and got the same error as you did.
Firstly mention the path to the feature file
features = {"src/cucumber/myfile.feature"}
Anyway, that didn't cause the error.
To just run your Cucumber runner class, all the dependencies you need are
cucmber-junit
cucumber-java and
junit.
I had an additional cucumber-guice which was creating the problem and once I removed it, the error went away and runner was executed successfully.
From the link to the image you have mentioned it looks like you are not using cucumber-guice but still I would recommend you remove other unnecessary cucumber dependencies and try again.
1, I ran into this too few days ago, its simple just remove cucumber-Spring from the dependency.
2 If that doesn't work try updating cucumber-core, cucumber-junit, and cucumber-java all version 1.2.3
I believe the issue is that many of the cucumber add-ins, such as cucumber-testng, cucumber-spring, and (in my case) cucumber-guice, expect the corresponding module they link to be included as well. But apparently the cucumber experts decided not to include this dependency in their pom.xml files, so the problem doesn't manifest itself until runtime.
So (to answer Eugene S's question under LING's answer) if you want to actually use guice with cucumber, you need to also add guice itself as a dependency.
This worked for me, I hope it will work for you as well.
Update your Cucumber dependencies in pom.xml
i.e
cucumber-java (1.2.2)
cucumber-jvm (1.2.2)
cucumber-junit (1.2.2)
And update your Junit dependency as well. (4.11).
The only reason for this error is the version of all the cucumber libraries are not same. It should be like this:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>4.2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.2.6</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
First Thing : We would request you to use Cucumber v >=4.0.0 as you are using pretty old dependency(v1.2.5) of Cucumber.
Key Point : We shall not mix direct & transitive dependencies specially their versions! Doing so can cause unpredictable outcome.
Solution: Please remove. cucumber-core, cucumber-java, cucumber-jvm-deps, gherkin and cucumber-html. They're transitive dependencies and will be provided by your direct dependencies.
You can add below set of cucumber minimal dependencies.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
<scope>test</scope>
</dependency>
After spending a lot of time on this issue, most of the errors I was receiving were due to dependencies and dependencies versions mismatch. Adding these dependencies to pom.xml file worked for me:
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-scala_2.11</artifactId>
<version>4.7.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>4.8.1</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java8 -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>4.8.1</version>
</dependency>

Unit Tests are getting failed in Java 8 to Java 11 Migration

I'm working on migrating my project from Java 8 to Java 11. So I used, Spring 5.1.0, Java 11, Eclipse 4.16 and Tomcat 9. I'm able to build the source successfully. But when it comes to Tests, they are getting failed.
Here is what I've in pom.xml for tests.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.27.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
And my test cases runs absolutely fine with the above dependencies in Java 8. But when I migrate the code to Java 11, I'm getting the below exception.
ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing
TestExecutionListener
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener#54e063d] to
prepare test instance [com.test.SomeTest2#4293943]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:122) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DefaultTestCDependencyInjectionTestExecutionListenerontext.java:122) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DefaDefaultTestCDependencyInjectionTestExecutionListenerontextultTestContext.java:122) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) ~[Spring-test-5.1.0.RELEASE.JAR:5.1.0.RELEASE]
Sample Test Class Structure I've
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:test-context.xml"})
public class SomeTestClass {
...
}
Which is getting failed because of the mentioned exception. But, I did some research and found a workaround i.e
to change from:
#ContextConfiguration(locations = {"classpath:test-context.xml"})
to this:
#ContextConfiguration(locations = {"classpath*:/spring/test-context.xml"})
And it works. But the problem is that I'm not allowed to edit the source code. How to achieve it?
If the directory that contains the "spring" directory is what is in your classpath, and not the "spring" directory itself, then this is not a "workaround", but a "fix". If you're not allowed to change anything, then you can't fix anything either.
According to your fix, it seems that test-context.xml is now located at classpath*:/spring/test-context.xml. Therefore, you can try adding the spring folder where test-context.xml is into the class path as well, and then it should work. No code changes needed for this solution. You can read how to do it at How do I add a directory to the eclipse classpath?

"linkablecontainer not found" when using testcontainers

I want to use testcontainers (https://www.testcontainers.org/usage.html)
So I imported the corresponding Maven dependencies:
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-xe</artifactId>
<version>1.10.1</version>
<scope>test</scope>
</dependency>
Then I right clicked the docker icon on taskbar -> Settings -> General
and checked the item:
Expose daemon on tcp://localhost:2375 without TLS
Set the environment variables as described on testcontainers site:
DOCKER_CERT_PATH=C:\Users\username\.docker
DOCKER_HOST=https://localhost:2375
DOCKER_TLS_VERIFY=1
And created a JUnit-test with the code:
#Test
public void test() {
OracleContainer oracleXE = new OracleContainer();
...
However I got the error:
Error:(82, 27) java: cannot access org.testcontainers.containers.traits.LinkableContainer
class file for org.testcontainers.containers.traits.LinkableContainer not found
I've googled for "linkablecontainer not found" and for
"org.testcontainers.containers.traits.LinkableContainer not found" but with no results.
Any ideas what went wrong?
Different case than yours but I got the same error and it seems that it's not that common.
In my case I got the same error when the dependency for the database was not in scope test but the dependency for the testcontainers it was.
For example on pom.xml
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mssqlserver</artifactId>
</dependency>
I forgot to remove <scope>test</scope> from the testcontainers and after that the error was gone.
I guess there isn't a global solution to this error but the cause could be some misconfiguration on pom.xml.

Selenium program throws compilation error org.openqa.selenium.internal.Killable cannot be resolved

I've created a new Java project using Selenium and Maven. This is the relevant content in pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
I then created this basic Java program that uses Selenium framework:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Start {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.firefox.marionette", "D:\\geckodriver.exe");
WebDriver driver = null;
try {
driver = new FirefoxDriver();
String baseUrl = "https://www.google.co.in/";
driver.get(baseUrl);
} finally {
driver.close();
}
}
}
However, I'm getting this compilation error:
The type org.openqa.selenium.internal.Killable cannot be resolved. It
is indirectly referenced from required .class files
Can someone pls suggest where am I going wrong?
Remove this dependency from pom.xml:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.8.1</version>
</dependency>
You are using a vesrion 2.45.0 of Selenium java bindings (published on February 2015):
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
If you examine compile dependencies of the above package: https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/2.45.0
you will see that this package depends on selenium-chrome-driver 2.45.0 package. Maven will resolve this dependency automatically during compilation, so you don't need to declare this package in the pom.xml. file.
But if you declare this package selenium-chrome-driver as a dependency directly in the pom.xml file, using different version (the newest 3.8.1), then maven will be using this version 3.8.1 instead of 2.45.0 during compilation, and this will cause the error - wrong, incompatibile jar library is used.
Please Check in your POM.XMl file that you might have 2 dependency for "selenium-java" or
"selenium-chrome-driver" dependency for two different version.
I also got same error. If you want to resolve this, follow these steps:
Go to the path location of pom.xml
Right-click and select Give access to
Click on Remove access
Click on Change sharing permission
In the dropdown menu, select Everyone
For Permission level, choose Read/Write
Save the XML file.
It should work now.
add this dependency:
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.12.0</version>
</dependency>
Add the following dependencies
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
<scope>test</scope>
</dependency>

Jena NoClassDefFoundError with Maven

I have a Java Maven project that I developed a while ago and that doesn't work anymore. It uses a parent pom together with another Maven project in which I think the Jena version was changed and it also uses an external library that uses Jena. The Maven dependency is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
When I execute my tests I get the following errors:
java.lang.NoClassDefFoundError: Could not initialize class
com.hp.hpl.jena.query.ARQ
java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory
at org.openjena.riot.system.PrefixMap.add(PrefixMap.java:54)
at com.hp.hpl.jena.sparql.util.MappingRegistry.addPrefixMapping(MappingRegistry.java:33)
at com.hp.hpl.jena.query.ARQ.init(ARQ.java:449) [...]
The errors are not thrown by my code directly but by the library I include. Can I prevent this by downgrading the Jena version in the parent pom or what can I do here?
P.S.: I now have a minimal code example that reproduces the error (java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory):
import org.junit.Test;
import com.hp.hpl.jena.query.ARQ;
public class DependencyTest
{
#Test
public void testARQ()
{
ARQ a = new ARQ();
}
}
And I guess it comes from this dependency:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
I know there is probably a factory instead of a constructor but I guess this still shows where the problem is.
P.S.: I noticed that I had the dependencies "jena", "arq" and "jena-arq":
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
So I thought maybe I have too much overlapping dependencies and commented out "jena" and "arq". But I still get the error
java.lang.NoClassDefFoundError: Could not initialize class com.hp.hpl.jena.query.ARQ
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.<init> [...]
I also tried out forcing a non-snapshot-version 2.9.0-incubating, but then I still get the NoClassDefFoundError with and without using the "jena" and "arq"-dependencies.
P.P.S.:
I still get the same error even when I use the following dependencies:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.7.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-iri</artifactId>
<version>2.7.0-incubating</version>
</dependency>
You can search for the missing class using the Maven Central search application
http://search.maven.org/#search|ga|1|fc%3A%22com.hp.hpl.jena.query.ARQ%22
It demonstrates that the dependency you appear to be missing is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.6.0</version>
</dependency>
Doesn't appear to be a version 2.6.4, but you're probabily best advised to go for a more modern version (This project was recently donated to apache)
Instructions for using Apache Jena with Maven are here:
incubator.apache.org/jena/download/maven.html
Specifying ARQ 2.9.0 as a dependency in your project pom.xml will pull in the other Jena components that you need.
I finally resolved this error by excluding the "jena"-Dependency brought in as a transitive dependency from some library.

Categories