I\'ve got a Cucumber Step class that i\'m attempting to initialise a page model for all scenarios. So I added a @Before annotated method :
@Before()
private void
There is an accepted answer to this question, but I wanted to point out the comment made by Matt Watson which solved the issue for me and for which I have not seen similar advice elsewhere:
I've had a play about with some of my cucumber-jvm tests and I think I've spotted it. Your
@Beforemethod should bepublicrather thanprivate
The @Before method must be public.
I know this issue is old, but in case somebody runs into the same problem using IntelliJ:
Check the "Glue"-Property in your Run/Debug Configuration.
This is the list of paths that Glue (the autowirering-system from Cucumber) uses to find Classes to wire.
It seems like IntelliJ is generating this property, if it is not specifically defined in the the Template for "Cucumber Java"-Configs.
I don't know how it is generated, but for me the package that contains my class with the Before-Method in question did not exist. After adding it everything worked normally.
Edit:
Found out more background-info. The IntelliJ Cucumber Plugin does not use Junit oder TestNG, but his own implemented runner suite. This runner does NOT interepret the Annotation-Based configurations from Cucumber, only the ones from the Cucumber Property-File or System-Properties. The TestNG-Suite however always overwrites the Glue-Path, not matter if it was actually set or is present. The most consistent way I found was to configure it both using annotations and properties. That way you are always sure that you're config ist used (Gradle-Runner, TestNG-Runner, IntellijCucumber-Runner)
In my case, it has worked when I put "dryRun = false" in the Cucumber options.
Make sure you are using cucumber.annotation.Before rather than org.junit.Before. Cucumber will not process JUnit annotations. (More information in the Scenario Hooks section of this blog post.)
Make sure your @Before method is public, not private.
In my case worked the addition of a package where @Before was defined to the glue parameter:
@CucumberOptions(glue = {"cucumber.hook", "cucumber.steps"})
Hello I know that is an old post, but none of these solutions work for me. So I'm going to share my solution.
I created the class Hooks under the package: com.mycompany.automation.util
package com.mycompany.automation.util;
import com.mycompany.automation.rest.database.AS400DBManager;
import cucumber.api.java.After;
import java.sql.SQLException;
/**
* @author <a href="mesaj@mycompany.com">Julian Mesa</a>
* @version 0.1.0
* @since 0.1.0
*/
public class Hooks {
@After
public void beforeScenario() throws SQLException, ClassNotFoundException {
System.out.print("Closing connection.");
AS400DBManager.getInstance().closeConnection();
}
}
and then I specified the package in the glue options in the runner:
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
features = "src/test/resources/features",
glue = {"com.mycompany.automation.features.steps",
"com.mycompany.automation.util"}
)
And it worked.