Hot questions for Using Cucumber in command line interface

Top Java Programmings / Cucumber / command line interface

Question:

Problem Cucumber can't find step definitions when run with a CLI runner, but it can find it when running with the junit runner. That is, when running cucumber-jvm from a linux command line, the feature file is found, but the step definitions file is not found, producing the message, "Undefined scenarios: src/test/java/com/logic/testing/ClassifyDocuments.feature:8"(See bottom for full message)

However, running via Maven, e.g. 'mvn test', the step definitions are found and the test executes as expected. I've already reviewed similar questions ad nauseum and would appreciate any help before I go bald. - Do the files need to be organized differently, e.g. using a 'resources' directory? - Is the glue parameter, com.logic.testing, not correct? - Is there a problem with the classpath?

Details Here's the command line statement being issued while in the 'auto-test' folder: java -cp "/usr/local/bin/cucumber/cucumber-core-1.2.5.jar:/usr/local/bin/cucumber/*:." cucumber.api.cli.Main -g com.logic.testing src/test/java/com/logic/testing/ClassifyDocuments.feature -s

Code is organized like so: auto-test/   src/test/java     com.logic.testing       StepDefinitions.java       ClassifyDocuments.feature   src/main/java     com.logic.testing       AutoTestController.java (contains a class that is referenced by StepDefinitions.java)   target/test-classes/com/logic/testing/     StepDefinitions.class   target/classes/com/logic/testing/     AutoTestController.class

Within /usr/local/bin/cucumber/ is: cucumber-core-1.2.5.jar cucumber-java-1.2.5.jar cucumber-jvm-deps-1.05.jar gherkin-2.12.2.jar

ClassifyDocuments.feature file:

Feature: Classify documents in a package
  As an auditor
  I want to use software
  So that I don't have to manually identify subdocuments

Scenario: execute workflow case2 test
Given the workflow case2 test can be configured
And I have been authenticated
When two jobs are submitted with different SLA duration
And the jobs are processed
Then the packages with the shorter SLA duration are completed first

StepDefinitions.java file:

package com.logic.testing;

import java.io.File;

import org.junit.Assert;

import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class StepDefinitions {

    AutoTestController  atc;
@Given("^the workflow case2 test can be configured$")
    public void the_workflow_case2_test_can_be_configured() throws Throwable {
        atc = new AutoTestController();
        atc.log("~Looking for configuration", log_src);
        Assert.assertTrue(atc.getAutoTestConfig("workflow_case2"));
    }

    @When("^two jobs are submitted with different SLA duration$")
    public void two_jobs_are_submitted_with_different_SLA_duration() throws Throwable {
        Assert.assertTrue(atc.two_jobs_are_submitted_with_different_SLA_duration());
    }

    @And("^the jobs are processed$")
    public void the_jobs_are_processed() throws Throwable {
        Assert.assertTrue(atc.processJobs());
    }

    @Then("^the packages with the shorter SLA duration are completed first$")
    public void the_packages_with_the_shorter_SLA_duration_are_completed_first() throws Throwable {
        Assert.assertTrue(atc.checkPackageCompletionTimes("QC_CLASSIFICATION", "READY", 10, 300));
    }
}

Error returned after executing the command line statement (yes, it does start with 'UUUUU'):

UUUUU

Undefined scenarios:
src/test/java/com/logic/testing/ClassifyDocuments.feature:8 # Scenario: execute workflow case2 test

1 Scenarios (1 undefined)
5 Steps (5 undefined)
0m0.000s


You can implement missing steps with the snippets below:

@Given("^the workflow case(\\d+) test can be configured$")
public void the_workflow_case_test_can_be_configured(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^I have been authenticated$")
public void i_have_been_authenticated() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^two jobs are submitted with different SLA duration$")
public void two_jobs_are_submitted_with_different_SLA_duration() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^the jobs are processed$")
public void the_jobs_are_processed() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^the packages with the shorter SLA duration are completed first$")
public void the_packages_with_the_shorter_SLA_duration_are_completed_first() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

Answer:

Cucumber scans the class path for glue.

So looking at a glance I'd say that your -cp is wrong. When executed from auto-test I would expect it to include ./target/classes/ and its descendent's rather then ..

Question:

I'm new to using Command Line Interface. So I just have a question on how to invoke the runner class of the cucumber using CLI technique.

I have a Java program which contains a main method. When testers pass the argument which is test case, it will fetch the feature file. The java program invoke a custom made API which will fetch the correct feature file.

Next I'll have to invoke the Cucumber runner class to execute the test case. I need to pass this particular feature file as the argument. Two questions, Can we invoke the runner class from a different main method. I did some research and I was not able to find a concrete answer. Two questions,

  1. cucumber.api.cli.Main.main(arguments); So how do i specify the jar location of my runner class.

    `FeatureFileCreation.main("xxxxx"); - API that fetches the right feature file String[] arguments = {"-", ""}; cucumber.api.cli.Main.main(arguments);

    • How do I specify where my jar is located? How can I pass my feature file?`
  2. Should I create a main method in the runner class, something like this? For the sake of using CLI,Since I need to create a runnable jar. I should have a main method in my runner class.

`

@RunWith(Cucumber.class)
@Cucumber.Options(features="C:/Users/IBM_ADMIN/Desktop/CRAutomation/CR Regression1/src/My.feature",glue={"bell.canada.step.definition"})

public class AutomationRunnerAction { 

    public void main(){
    }
}`

Please note that, Getting the right feature file is 1 java API. I will invoking that API from one main method of one java program. The runner class with step definition and methods are a diff java program.


Answer:

Unfortunately the accept answer is not correct. If you look at the source of Main.main() you'll notice that it contains: System.exit(exitstatus) which terminates the system.

The proper way to run the commandline programatically would be to use Main.run() like this:

String [] argv = new String[]{ "-g","","./src/main/java/featureDetails/Testing.feature"};
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
byte exitstatus = Main.run(argv, contextClassLoader);