7

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();
}
Craig
  • 456
  • 5
  • 14
  • Have you compiled your step definition file? – fg78nc Jul 10 '17 at 22:49
  • yes, the step definition file was compiled – Craig Jul 10 '17 at 22:51
  • where is compiled file? I think you are not referring to correct path. – fg78nc Jul 10 '17 at 22:55
  • the compiled StepDefinitions file is in target/test-classes/com/logic/testing/ – Craig Jul 10 '17 at 23:08
  • Where are you executing your code from? – fg78nc Jul 10 '17 at 23:16
  • the code is being executed in auto-test, the parent folder of target/ – Craig Jul 10 '17 at 23:17
  • Then maybe it should be -g target/test-classes/com/logic/testing/ ? – fg78nc Jul 10 '17 at 23:19
  • no, that doesn't work either – Craig Jul 10 '17 at 23:32
  • Just to find out the problem please put your compiled step definition in the child folder (let's say "steps", so you will it would be auto-test/steps) of auto-test and try to run with -g steps option from auto-test. – fg78nc Jul 10 '17 at 23:34
  • @Craig What happens when you run this command without the '-s' option at the end? I think the last parameter needs to be the feature file path and all options like -g,-s.-p etc have to before the path. – Grasshopper Jul 11 '17 at 04:15
  • @fg78nc, I get the same result when placing the compiled step definitions in auto-test/steps, 'undefined scenarios'. The command used was: java -cp "/usr/local/bin/cucumber/cucumber-core-1.2.5.jar:/usr/local/bin/cucumber/*:." cucumber.api.cli.Main -g steps . -s – Craig Jul 11 '17 at 13:36
  • @Grasshopper: same result when the -s option is moved earlier in the statement. -s is just echoing the error (without it, the response excludes 'undefined errors' text). – Craig Jul 11 '17 at 13:37
  • @Craig what is the use of dot after steps? – fg78nc Jul 11 '17 at 13:59
  • @fg78nc, it's the path to the feature file – Craig Jul 11 '17 at 14:24
  • Try without it, please – fg78nc Jul 11 '17 at 14:26
  • @fg78nc, it returns 'Got no path to feature directory or feature file' – Craig Jul 11 '17 at 14:27
  • Can you archive and upload your project? – fg78nc Jul 11 '17 at 14:30
  • @Craig try `java -cp "/usr/local/bin/cucumber/cucumber-core-1.2.5.jar:/usr/local/bin/cucumber/*:." cucumber.api.cli.Main -g com.logic.testing .` from `auto-test` directory. Put compiled step definitions file into `auto-test/com/logic/testing/` Compiled file package should be `package com.logic.testing;` – fg78nc Jul 11 '17 at 16:22
  • thanks, but that fails too. Do you use the CLI runner for testing or the junit runner? – Craig Jul 11 '17 at 19:30

3 Answers3

1

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

M.P. Korstanje
  • 10,426
  • 3
  • 36
  • 58
  • Agreed: my classpath is lacking. However, it's not feasible to list the classpath because it relies on many directories and it's difficult to determine which are needed. To meet the requirement of running cucumber from the command line, I'm changing approach: using Maven, a fat jar will be created with a main class that calls the cucumber main class. Then the jar can be executed on a server that does not have Maven installed... – Craig Jul 27 '17 at 17:02
  • Have a look at the maven app assembler plugin. I find it to be less cumbersome/complicated then uber jars. You can rsync only the parts that have changed. http://www.mojohaus.org/appassembler/appassembler-maven-plugin/ – M.P. Korstanje Jul 31 '17 at 22:15
1

I have downloaded cucumber-core.jar into c:\cukes folder and my test classes are in target/test-classes folder as I am using maven. also my maven dependencies are in .m2 folder of my profile. below command line code works fine for me.

java -cp "c:/cukes/*;./../../.m2/*;target/test-classes" cucumber.api.cli.Main --glue "stepdefinitions" src/test/resources/features/sample.feature

I guess, you are getting the issue may be because of class path, try the following

java -cp "/usr/local/bin/cucumber/*;target/test-classes;target/classes" cucumber.api.cli.Main -g "com.logic.testing" src/test/java/com/logic/testing/
Murthi
  • 5,299
  • 1
  • 10
  • 15
1

Maybe your version of cucumber-java and cucmber-junit are old? Updating dependencies helped me with this problem. And reload project afterwards.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103