Cucumber not loading supporting classes

1,766 views
Skip to first unread message

Michael Cunningham

unread,
Nov 5, 2018, 11:37:48 AM11/5/18
to Cukes
I have a Cucumber/Java project whose glue package is x.y.z

test.resources.features/dropDatabaseFeature.feature which makes use of methods in the corresponding java steps code dropDatabaseSteps.java
test.resources.features/transferFileFeature.feature which makes use of methods in the corresponding java steps code transferFileSteps.java

At test.java.x.y.z.databaseSteps/dropDatabaseSteps.java I use @ContextConfiguration=CucumberConfig.class
At test.java x.y.z.fileSteps/transferFileSteps.java  I use @ContextConfiguration=CucumberConfig.class
and get an error saying both classes attempt to configure the spring context.   If only 1 class has the @ContextConfiguration=CucumberConfig.class it works ok.

Since I do not want to hide the SpringContext setup in any one feature file, since I will eventually have many, I created test.java.x.y.z.SpringContextSetup.java, and empty class which had @ContextConfiguration=CucumberConfig.class before the class statement.  This resulted in nullPointerException for the injected objects, so the Spring container config was not being configured.  Why is this?  It because there's no features file using the SpringContextSetup class and so the class is not being loaded at runtime?

Björn Rasmusson

unread,
Nov 5, 2018, 3:04:26 PM11/5/18
to Cukes
Michael Cunningham wrote:
I have a Cucumber/Java project whose glue package is x.y.z

test.resources.features/dropDatabaseFeature.feature which makes use of methods in the corresponding java steps code dropDatabaseSteps.java
test.resources.features/transferFileFeature.feature which makes use of methods in the corresponding java steps code transferFileSteps.java

Note, all step definitions in all glue classes are available to every feature file. I say this because your conversion of collection all step definitions used in one glue class most likely, sooner of later, will result the both X.feature and Y.feature contain the step "Given Z", and defining a step definition for the step "Given Z" in more than one glue class will result in an error.
 

At test.java.x.y.z.databaseSteps/dropDatabaseSteps.java I use @ContextConfiguration=CucumberConfig.class
At test.java x.y.z.fileSteps/transferFileSteps.java  I use @ContextConfiguration=CucumberConfig.class
and get an error saying both classes attempt to configure the spring context.   If only 1 class has the @ContextConfiguration=CucumberConfig.class it works ok.

Since I do not want to hide the SpringContext setup in any one feature file, since I will eventually have many, I created test.java.x.y.z.SpringContextSetup.java, and empty class which had @ContextConfiguration=CucumberConfig.class before the class statement.  This resulted in nullPointerException for the injected objects, so the Spring container config was not being configured.  Why is this?  It because there's no features file using the SpringContextSetup class and so the class is not being loaded at runtime?
 
Only @ContextConfiguration annotations on glue classes are considered by Cucumber-JVM. Glue classes are classes with, at least on, step definition (Given, When, Then etc.) or hook (cucumber.api.java.Before, cucumber.api.java.After etc). Your SpringContextSetup class must contain no step definition or hook, otherwise its @ContextConfiguration had been used by Cucumber-JVM.

/Björn

Michael Cunningham

unread,
Nov 12, 2018, 5:02:48 AM11/12/18
to Cukes
Many thanks Björn. Makes sense now. I moved @ContextConfiguration into more general, shared  CucumberHooks class.

Michael
Reply all
Reply to author
Forward
0 new messages