Cant run feature in Cucumber - java

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>

Related

IntelliJ can't find tests if package name exists in src/main/java

I'm trying to introduce Unit tests to our system, and have run into a problem with Junit not finding test.
I have these 3 tests:
When I run all tests in the module:
It finds X and Y tests, but not Z:
The difference between the 3 is only in the package name:
The package com.exlibris.x (XTest) doesn't exist in the project
The package com.exlibris.core.infra.svc.api.flags (YTest) exists in a different module in the project (that is outputted to a different jar file)
The package com.exlibris.repository.web.mms.publishing (ZTest) exists in the same module under the src/main/java
My pom.xml has the following dependencies (inherited from the parent pom):
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
EDIT: These are my run configurations
So it turned out there were a few issues (some of them are likely not related to the specific question, but still best practices).
What helped for me was:
Consolidate dependency management of JUnit by importing junit-bom
Upgrading maven-surefire-plugin to latest version
Removing unnecessary configs from maven-surefire-plugin (my configuration tag is now empty)
Reloading the project in IntelliJ (Right click project -> Maven -> Reload project)
Thanks a lot to khmarbaise and Kathrin Geilmann for the help :)

NoClassDefFoundError with EncryptedDocumentException

I am building a java program that opens an excel document. While in the IDE everything works, when i execute the code from the built jar everything falls apart and it gives me the following exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/EncryptedDocumentException
at com.exceltogpx.Main.main(Main.java:19)
It stops the execution at the import of the class
import org.apache.poi.EncryptedDocumentException;
and it's driving me crazy
it's a maven build, dependencies imports following
<dependencies>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
I think something may be wrong with either the classpath or the way I create the build with maven, but it seems I can't find the issue itself
Found out the answer to the question
I am a newbie maven user and I had never worked with pom a before. Be sure to specify a goal for your project and to specify that it will be a "jar-with-dependencies"
Here's the guide I used to fix my pom file (I followed the second part of the guide, the one that uses Apache maven assembly plugin)
Other suggestions for others using maven for the first time. If you used any external libraries so you have dependencies, in the log you should see the download of those libs. Otherwise it means you probably did something wrong with the pom... The guide I linked works pretty good and explains what the main tags do.

Configure classpath in eclipse for JUnit 5 with Maven

Eclipse (2018-09) supports JUnit 5 tests only if the JUnit 5 engine is present on the classpath of the project.
Now I have two possibilities in my Maven project:
Add it to the project via an eclipse JUnit Library
and add only the API to the dependencies
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
Add both the engine and the API to to my Maven pom
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
If I do the former, then everybody using eclipse has to do this himself.
If I do the later, then I pollute my (test) compile time classpath with implementation classes, that I (and users of other IDEs) might use instead of the API classes. Also this might cause conflicts with the IDE that might need a different version of the engine, than the one on the cp. IIRC that was the whole point why API and engine have been split up in the first place.
Unfortunately there is no testRuntimeOnly scope in Maven (like there is in gradle).
TLDR: Which way is the correct way to configure JUnit 5 for eclipse for a Maven project?
If I do the former, then everybody using eclipse has to do this himself.
I assume you intended to give an eclipse user a chance to execute a test by right clicking a JUnit test class and selecting Run as > JUnit Test. I don't know if this is the right way but to do so you need to add an additional dependency besides JUnit Jupiter API/Engine, which is JUnit Platform Launcher.
For example:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.2</version>
<scope>test</scope>
</dependency>
Not sure if its related but I am using maven-surefire-plugin Version 2.22.1 which throws ClassNotFoundException if JUnit Platform Launcher is missing.

java to scala to spark. NoClassDefFoundError

I have some scala utility classes for loading csv files and manipulating as DataFrames. They work fine from scala.
I just tried using the classes by invoking my scala util from java. I got the following exception.
java.lang.NoClassDefFoundError: scala/Product$class
at org.apache.spark.SparkConf$DeprecatedConfig.(SparkConf.scala:723)
at org.apache.spark.SparkConf$.(SparkConf.scala:571)
Both my java and scala projects are maven projects
My java application pom.xml just has one dependency, the dependency to my scala util.
My scala util initiates a SparkSession loads the csv files and manipulates the data in DataFrames. It has the following dependencies (which work fine when runningas standalone scala)
<!---spark-->
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql_2.11 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-streaming_2.11 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-mllib_2.11 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.11</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-graphx_2.11 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-graphx_2.11</artifactId>
<version>2.2.0</version>
</dependency>
Can someone offer a hint as to what I am missing
UPDATE: This is not a duplicate question.
Scala is not invoked directly in java. Even so I added a property to the java pom:
<scala.version>2.11.11</scala.version>
This made no difference. The property is already in the scala pom

How to run single scenario

Everytime I'm trying to run a single feature file or a single scenario in a feature file, it create new configuration file in intellij. The Glue property is empty and the Feature or folder path is located on a specific feature file, the feature file of that scenario:
The errors after trying to run a single feature file or a single scenario is:
Undefined step: .... for every step in the feature file/scenario I'm trying to run.
Is there any sulotion to this problem instead of creating 1000 configurations?
My dependencies:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
You have two different versions of Cucumber involved. 1.2.4 and 1.2.5, is that the reason you're having problems?
IDEA adds "glue" automatically if your step definition file is located under named package (not right under test->java).
I checked this example http://github.com/czeczotka/cucumber-jvm-maven with your dependencies and it seems to work fine.

Categories

Resources