@Before doesn't execute in java Cucumber Step
Make sure you are using
cucumber.annotation.Before
rather thanorg.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
, notprivate
.
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="[email protected]">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.
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
@Before
method should bepublic
rather thanprivate
The @Before
method must be public
.