Search code examples
automated-testscucumber-javacucumber-junit

Getting Error for @CucumberOptions and seems cucumber.class deprecated


[My Test Runner Class][1]

I tried to create TestRunner Class (using Eclipse-Java11-Cucumber-Maven); however getting error for CucumberOptions annotation and seems Cucumber.class is deprecated. Please let me know how to solve this? Please see attached image.

Also, below is the Test Runner Code:

package TestRunner;

import io.cucumber.junit.platform.engine.Cucumber;
import io.cucumber.junit.platform.*;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;


@RunWith(Cucumber.class)
@CucumberOptions(
        feature = "src\\test\\java\\AppFeatures",
        glue = "StepDefinitions")

public class Runner {
    
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  [1]: https://i.sstatic.net/etCZ6.png

Solution

  • If you look at the source code for io.cucumber.junit.platform.engine.Cucumber you'll see that:

    https://github.com/cucumber/cucumber-jvm/blob/main/junit-platform-engine/src/main/java/io/cucumber/junit/platform/engine/Cucumber.java

    package io.cucumber.junit.platform.engine;
    
    import org.apiguardian.api.API;
    import org.apiguardian.api.API.Status;
    import org.junit.platform.commons.annotation.Testable;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * Test discovery annotation. Marks the package of the annotated class for test
     * discovery.
     * <p>
     * Maven and Gradle do not support the
     * {@link org.junit.platform.engine.discovery.DiscoverySelectors} used by
     * Cucumber. As a workaround Cucumber will scan the package of the annotated
     * class for feature files and execute them.
     * <p>
     * Note about Testable: While this class is annotated with @Testable the
     * recommended way for IDEs and other tooling use the selectors implemented by
     * Cucumber to discover feature files.
     * <p>
     * 
     * @deprecated Please use the JUnit Platform Suite to run Cucumber in
     *             combination with Surefire or Gradle. E.g: <code><pre>{@code
     *package com.example;
     *
     *import org.junit.platform.suite.api.ConfigurationParameter;
     *import org.junit.platform.suite.api.SelectClasspathResource;
     *import org.junit.platform.suite.api.Suite;
     *
     *import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
     *
     *&#64;Suite
     *&#64;SelectClasspathResource("com/example")
     *&#64;ConfigurationParameter(
     *   key = GLUE_PROPERTY_NAME,
     *   value = "com.example"
     *)
     *public class RunCucumberTest {
     *}
     *}</pre></code>
     * @see        CucumberTestEngine
     */
    @API(status = Status.DEPRECATED)
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @Testable
    @Deprecated
    public @interface Cucumber {
    
    }
    
    

    If you are using Maven you can download the sources with mvn dependency:sources. Though typically you won't have to as your IDE can do this for you.

    Now there are a few things to observe here:

    1. If you search for the JUnit Platform you'll find that it is part of JUnit 5. However @RunWith is part of JUnit 4.

      This means that you are mixing different framework versions together. You can not expect that to work.

    2. There is a code snippet in the Javadoc that suggests the correct way to use Cucumber with JUnit 5.

    package com.example;
    
    import org.junit.platform.suite.api.ConfigurationParameter;
    import org.junit.platform.suite.api.IncludeEngines;
    import org.junit.platform.suite.api.SelectClasspathResource;
    import org.junit.platform.suite.api.Suite;
    
    import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
    
    @Suite
    @IncludeEngines("cucumber")
    @SelectClasspathResource("com/example")
    @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example")
    public class RunCucumberTest {
    }
    

    You should consider using that.

    Each open source project is different but it's also good to look at the source. Documentation may be included:

    https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine

    And since this is a JUnit 5 engine, the JUnit 5 documentation is good to read too.

    https://junit.org/junit5/docs/current/user-guide/