Search code examples
seleniumcucumbercucumber-javacucumber-junit

When runing cucumber from main method giving me skeleton of my features


When running the cucumber test runner from main class as per below screenshot just giving me the skeleton of the feature to define the steps. Whereas I have already written the code to achieve the steps.

Attaching screenshot to understand my code as well project structure. Mainjarvish.java is the main file from where I am trying to run the TestRunner.java.

main method


Solution

  • It fails because your class test/java/stepDefinitions/Contacts is not in the same class path scope as src/main/java/MainJarvish.

    Assume following structure.

    src/main/java/MainJarvish.java
    src/test/java/stepDefinitions/Contacts.java
    src/test/java/FeatureList/Smoke.feature
    

    MainJarvish.java

    import cucumber.api.cli.Main;
    
    public class MainJarvish {
        public static void main(String[] args) throws Exception {
            String[] options = {"--glue", "stepDefinitions",
                "src/test/java/FeatureList/Smoke.feature"};
            byte exitcode = Main.run(options, Thread.currentThread().getContextClassLoader());
            System.out.println("exitcode = " + exitcode);
        }
    }
    

    Contacts.java

    package stepDefinitions;
    
    import cucumber.api.java.en.Given;
    
    public class Contacts {
        @Given("^Submit message for execution$")
        public void submitMessageForExecution() throws Throwable {
            System.out.println("submitMessageForExecution");
        }
    }
    

    Smoke.feature

    Feature: example feature
    
      Scenario: describe the test scenario
        Given Submit message for execution
    

    compile the classes

    mvn clean compile test-compile
    

    it generates the class files

    target/classes/MainJarvish.class
    target/test-classes/stepDefinitions/Contacts.class
    

    When you now execute ManJarvish

    mvn exec:java -Dexec.mainClass=MainJarvish
    

    the class Contacts is not on the class path and therefor the step definition is not found.

    [INFO] --- exec-maven-plugin:1.6.0:java (default-cli)
    ...
    Undefined scenarios:
    src/test/java/FeatureList/Smoke.feature:3 # describe the test scenario
    
    1 Scenarios (1 undefined)
    1 Steps (1 undefined)
    

    As MainJarvish is located in src/java at its execution only the project classes below target/classes are added to the class path.

    If the class Contacts would be on the class path, e.g. by changing the class path scope

    mvn exec:java -Dexec.mainClass=MainJarvish -Dexec.classpathScope="test"
    

    now are the classes below target/classes and below target/test-classes added to the class path and the step definition is found.

    [INFO] --- exec-maven-plugin:1.6.0:java (default-cli)
    submitMessageForExecution
    ...    
    1 Scenarios (1 passed)
    1 Steps (1 passed)
    

    Depending on what you want to achieve you might consider to move MainJarvish to test/java or create a test runner class.

    src/test/java/TestRunner.java

    import org.junit.runner.RunWith;
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(
            glue = "stepDefinitions",
            features="src/test/java/FeatureList/Smoke.feature"
    )
    public class TestRunner {
    }
    

    then you can run your feature test as

    mvn clean test
    

    output

    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running TestRunner
    submitMessageForExecution
    
    1 Scenarios (1 passed)
    1 Steps (1 passed)