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

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?

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>

How to upgrade JUnit version in Intelliji (from 4.11 to latest one)

It's not really coding question but I'm stuck on it. I start new Java project in Intelliji and add tests with JUnit5. In this tests I'm using #Role annotation for FakeSftpServerRule github library.
I added all jupiter dependencies I just can thought about them in the pom file:
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
It's the way to initialize the fake ftp:
#Rule
public final FakeSftpServerRule fakeSftpServer = new FakeSftpServerRule()
.addUser(username, password)
.setPort(port);
And I'm using regular other attributes, like #BeforeAll and #Test...
Running the tests throw this error:
Internal Error occurred. org.junit.platform.commons.JUnitException:
TestEngine with ID 'junit-vintage' failed to discover tests at
org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:189)
at
org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:168)
at
org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
at
com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at
com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at
com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.junit.platform.commons.JUnitException: Unsupported
version of junit:junit: 4.11-beta-1. Please upgrade to version 4.12 or
later. at
org.junit.vintage.engine.JUnit4VersionCheck.checkSupported(JUnit4VersionCheck.java:39)
at
org.junit.vintage.engine.JUnit4VersionCheck.checkSupported(JUnit4VersionCheck.java:32)
at
org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:62)
at
org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:181)
Now, by navigate to source I found that #BeforeAll and #Test annotations point on junit-jupiter 5.5.2 but #Role point on junit 4.11 (beta-1).
How can I get rid from junit 4.11 and update it to the latest version?

NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass

I have the test that leads to error. I tried to execute it in the IntelliJ Idea 2018.3.2. All jupiter and junit dependencies have version RELEASE
The full text of error:
Dec 26, 2018 1:17:17 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.createAbortedExecutionPredicate(OpenTest4JAndJUnit4AwareThrowableCollector.java:40)
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.<clinit>(OpenTest4JAndJUnit4AwareThrowableCollector.java:30)
at org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector(JupiterThrowableCollectorFactory.java:34)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:68)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
the test has the following view
import biz.Services.msg.BookTimeMsgService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertTrue;
#ExtendWith(MockitoExtension.class)
public class MsgReceiverTest {
#Mock
BookTimeMsgService bookTimeMsgService;
#InjectMocks
MsgReceiver msgReceiver;
#Test
public void msgReceiverTest_bookTimeServiceShouldObtainMsg() {
assertTrue(true);
}
part of my pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>RELEASE</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
How to fix the issue?
I changed the version of jupiter and junit to 5.3.2 and the problem has gone
I was able to run the Junit 5 tests after adding the platform and launcher dependency:
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-commons -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
minimal required pom.xml setup for spring-boot 2.1.3 and junit 5.4.0 testing next:
<?xml version="1.0" encoding="UTF-8"?>
<project ...>
<parent>
<groupId>org.springframework.boot</g..roupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<junit-jupiter.version>5.4.0</junit-jupiter.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
use:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
instead
<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>
you can find more details here
Combos of Maven/Gradle, Eclipse/IntelliJ using Spring Boot/or not while including Junit4/Junit5 throw different flavors of this NoSuchMethodError. This leads to many different solutions all of which only fix a portion of people's problems. This long winded answer is an attempt to explain the root cause and steps you can take to fix your problem whichever flavor you are using.
Root cause:
At compile time, one version of ReflectionUtils (I've seen other classes here too) was used, and at runtime, a different one was used. The runtime version was expecting a certain method to exist, but either it didn't exist, or it existed with a different signature (number/type of parameters)
Note, when I say compile time, this can mean (and usually does) mean the version of ReflectionUtils that was used to compile a third party jar inside your classpath.
The solution will always be to change or exclude some version of some jar. The trick is to find which jar and which version.
To solve:
First, identify what version of ReflectionUtils (or other class it is complaining about) is being used at runtime. This is fairly easy. In eclipse, do an Open Type, in IntelliJ, use a navigate/go to class. Make sure to fully qualify the path. If you have exactly 1 version on the path, this is good. If you have more than on version on the path, your first action to take is to modify your build path to exclude one of the versions.
If you cannot modify the build path, even temporarily, it is harder to identify what version of ReflectionUtils is being used at runtime. Since JUnit 4 and 5 begins and dies before you can run any of your code, you cannot insert a
CodeSource src = ReflectionUtils.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
System.out.println(jar.toString());
}
to get the location of the runtime jar. But you can try:
1 - create a fresh new project, a fresh pom, put 1 junit inside of it, and run it with the above code and see what comes out. This may work because whatever conflict was in the non working project has been removed. This may not work in which case move to #2. This also may give a false result because the runtime path of a fresh project may be different than that of the non working project. Try updating the pom of the fresh project to look more and more like that of the non working project. Remember, the goal is to find what version of the class is being used at runtime
2 - Use google. Yah I know you got here by using google but this time change your search to see if someone documented "This version of JUnit5 uses this version of ReflectionUtils at runtime" or "This version of IntelliJ puts this version of ReflectionUtils on the classpath" or "This version of Spring Boot is putting this version of ReflectionUtils on the classpath". Remember, the goal is to find what version of the class is being used at runtime
3 - Hijack a class (advanced). When the stacktrace comes out, you will see several classes in the stacktrace. Try to find the source code for the class on the internet and create that package and class in your project. Then insert the code above and try to run the test which was failing.
Once you have found which version of ReflectionUtils is used at runtime, you need to find out what version was used at compile time
Identify which jar ReflectionUtils lives inside.
In the stacktrace which pops out when you run your test, identify the top class and method which died trying to use ReflectionUtils. This is the code which was compiled with an incompatible version of ReflectionUtils. Find out what jar that class is inside. I'll call this jar the conflicting jar. Try and get a dependency tree for that conflicting jar. The dependency tree should indicate which version of the ReflectionUtils jar the conflicting jar was dependent on.
Once you have found which version of ReflectionUtils jar was used at compile time and runtime, you likely will see that they are different and also incompatible. At this point, you will have to
1 - change the version of the ReflectionUtils jar used at runtime to match the version of ReflectionUtils needed by the conflicting jar
2 - change the version of the conflicting jar to one which is dependent on the same version of the ReflectionUtils jar which is on the runtime path
3 - if the conflicting jar is a dependency of some parent jar in build path and not explicity included by your build path, try excluding the conflicting jar from the parent jar and add a newer version explicitly. This may solve the problem - but this is, of course, the type of behavior which created this mess in the first place.
Other actions you can take:
If you know that you are using both JUnit4 jars as well as JUnit5 in your project, try removing one or the other completely from the compile time and runtime paths. The Assert patterns of the two paradigms are different enough that you likely do not want to mix them in the same project as it could lead to confusion of which parameters are the expected results, the message, and the variable under test.

"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.

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

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;

Categories