Skip to content

Cucumber Spring is not finding my step definitions #835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JasonSmiley opened this issue Feb 12, 2015 · 17 comments
Closed

Cucumber Spring is not finding my step definitions #835

JasonSmiley opened this issue Feb 12, 2015 · 17 comments

Comments

@JasonSmiley
Copy link

Since there is no great documentation on how to use this, I will admit I am possibly setting up my environment incorrectly. However, I can launch the WebApp (I see building of resources in console), I can see cucumber finding my features to execute, but when it tries to actually execute them, it says my steps are not defined and test is ignored.

My code can be found in this stack over flow question http://stackoverflow.com/questions/28466479/cucumber-spring-is-not-finding-step-definitions

To be honest, it looks like a bug in the cucumber to me at the moment, but since there is no good documentation on this, I can't tell if I am doing something wrong. Please advise!

currently using 1.2.2 Cucumber API

@aslakhellesoy
Copy link
Contributor

I'm closing this issue since it sounds more like a question and not a bug report. Please see CONTRIBUTING for details.

Feel free to reopen if you can share some code to reproduce, preferably in a GitHub repo.

@JasonSmiley
Copy link
Author

It's a bug if my setup matches your example, which to me seems to be the case. If I am wrong, then agree. Sorry!

@aslakhellesoy
Copy link
Contributor

Ok we'll see when you supply code to reproduce!

@JasonSmiley
Copy link
Author

The code I am using is in the Stack Overflow.

Are you saying you need a github?

@brasmusson
Copy link
Contributor

@JasonSmiley In your case the problems seems to be that you tried to use class (static) methods as step definitions (as I answered on Stack Overflow)

@JasonSmiley
Copy link
Author

I will keep my feed back on stack overflow, but that wasn't the issue - it still can't find my functions

@JasonSmiley
Copy link
Author

Just for the record, I checked out your cucumber examples project and couldn't even get it to compile!

@brasmusson
Copy link
Contributor

@JasonSmiley That is really strange, because the spring-txn example is building and executing without problems on Travis, here is the latest Travis log, from line 6785 is the output from the spring-txn example.

@JasonSmiley
Copy link
Author

well, I checked out the full cucumber-jvm repo, opened spring-txn in intellije via the pom.xml, and then it couldn't compile - I thought it was funny because it didn't know what @CucumberOptions were....

Thanks so much for your support @brasmusson but I am going to give up on cucumber spring. I have already spent 2 days trying to get this to work and I can't waste anymore time on this.

@aslakhellesoy
Copy link
Contributor

@JasonSmiley you must open the root pom

@brasmusson
Copy link
Contributor

@JasonSmiley I copied the code from your question at stackoverflow and I basically find no problem with it with respect to "finding Step Definitions". I put it in a GitHub project, you can try it yourself if you want (the GitHub project also include the Cucumber-Java-Skeleton project changed to use cucumber-spring).

Some notes though:

  • The question "Cucumber-spring is not finding Step Definitions" is missleading, it is always cucumber-core that search for Step Definition, then cucumber-core passes the found Step Definition classes to the backend (and with cucumber-spring on the classpath, that would be cucumber-spring) with the intention "when I ask for a instance of any of this classes, create it for me please".
  • Currently the glue parameter on the runner class (on StackOverflow page) is specified as glue = "stepDefinitions.ItemServiceControllerTestsSteps", the glue parameter should specify base-package(s) for the search for Step Definition so it should be specified glue = "stepDefinitions" (I'm pretty sure it was that the first time I looked at the StackOverflow page).
  • I notice that the JUnit class has a @WebAppConfiguration annotation together with the @ContextConfiguration annotation, but the Step Definition class only has a @ContextConfiguration annotation. It has nothing to do with finding Step Definitions, but I you want to use whatever features the @WebAppConfiguration enables when running your features, you need to put it together with the @ContextConfiguration (or @ContextHierarchy) annotation on the Step Definition class with the @ContextConfiguration (or @ContextHierarchy) annotation.
  • Step Definition classes need a default constructor for the Spring Context to be able to instantiate them (as far as I can tell, I had to add one to the ItemServiceControllerTestsSteps to make it work)
  • Returning values from Step Definitions does not seem to ruin anything, but it is not useful since the value returned will not be used.
  • It seems that Step Definitions can be declared static, but the whole idea with Cucumber-JVM is that the Step Definition classes are created fresh for each Scenario.
  • To answer a questing on a comment you added and deleted here in this discussion. Yes, cucumber-spring can be used to connect different Step Definition class instances with @Autowire, see the example here were one Step Definition access a field on another Step Definition. Note that the Step Definition classes should not be declared a beans explicitly, cucumber-spring will add bean definition to the Spring Context itself, making sure that they get the scope cucumber-glue so they will be created fresh for each Scenario.

@JasonSmiley
Copy link
Author

Updated my glue to "com.dibs.inventory.stepDefinitions" to match my step package.

Removed @Webapllication annotation from JUnit and cucumber (wasn't required)

Added default constructor.

Note: The return statement was for a demo I was giving to show that a step function could still be used in a traditional JUnit setting (not all developers I work with are comfortable using cucumber).

so just to clarify, cucumber.xml needs a bean line for each step definition? That might explain why I am getting the following error:

java.lang.IncompatibleClassChangeError: Found class org.springframework.test.context.TestContext, but interface was expected
at cucumber.runtime.java.spring.CucumberTestContextManager.getContext(SpringFactory.java:215)

@brasmusson
Copy link
Contributor

@JasonSmiley You are not using Spring version 4, are you? I get the error:

java.lang.IncompatibleClassChangeError: Found class org.springframework.test.context.TestContext, but interface was expected
at cucumber.runtime.java.spring.CucumberTestContextManager.getContext(SpringFactory.java:215)

when using Spring v3.2.13 (I googled up springtestdbunit/spring-test-dbunit#46 when searching for the error message).
Cucumber-spring (v1.2.2) is built using Spring v4.1.4.RELEASE. Cucumber-JVM v1.1.5 was the last version built using Spring v3. However in versions v1.1.4-v1.1.8 Cucumber-spring had its share of issues (see #711), so I must recommend using Cucumber-JVM v1.2.x when using Spring.

No, cucumber.xml should not mention the step definition classes. But since Cucumber-spring adds bean definitions for them in the context, they can still @Autowire each other (in Cucumber-JVM v1.2.0 and later).

@JasonSmiley
Copy link
Author

Ok, I am not sure if I will be able to update my spring version - I am writing a code for a spring project which is currently using 3.2.0.RELEASE so I will find out if there is a reason we haven't upgraded.

EDIT: seems that the developers are scared of lose of functionality and tech debt in general for upgrading to 4.1.4+

@brasmusson
Copy link
Contributor

I guess that the options for using Spring v3 and Cucumber-JVM are:

  • Use Cucumber-JVM v1.1.3 (or maybe earlier):
    • cucumber.xml need to be named that and must be placed at the root of the classpath.
    • Do not use @ContextConfiguration annotations
    • @Autowire step definition classes to each other will work
    • The transaction support will work (add cucumber.api.spring to the glue paths and Before/After hooks will Start/Stop transactions).
    • The scope cucumber-glue will be available automatically in the spring context.
    • Annotations like @WebAppConfiguration, @DirtiesContextwill not be taken into account.
    • The Pretty formatter will in v1.1.3 crash with a NullPointerException.
  • Use Cucumber-JVM v1.1.5 (or maybe v1.1.4):
    • Every step definition class needs a @ContextConfiguration annotation.
    • "cucumber.xml" is named in the @ContextConfiguration annotations, and does not need to be named that.
    • Annotations like @WebAppConfiguration, @DirtiesContexttogether with the @ContextConfiguration annotation will be honored.
    • @Autowire step definition classes to each other will not work.
    • The transaction support will not work.
    • The scope cucumber-glue need to explicitly added in the "cucumber.xml"

@JasonSmiley
Copy link
Author

Thanks for this @brasmusson, I am going to push for upgrading our spring environment since I will be writing tests for the code, but in the meantime I am just going to be writing CRUD tests and I have been asked to write them in Pure JUnit by the rest of the team.

Eventually I will be writing Integration level tests and then cucumber will be much more important so I will make my decision on what cucumber to use then.

@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants