In Cucumber, we have Before & After hook – it will run before and after each scenarios.
Problem Statement: What if we have to open the database connectivity (only once) before executing the scenarios and closing the connection (only once) after running all scenarios.
One of the Solution – We can use @BeforeClass and @AfterClass (junit annotations) in Runner Class which will run once.
Refer Below Runner Class:
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src\\Test1.feature",
glue={"stepDefinition"}
)
public class RunnerClass {
@BeforeClass
public static void beforeclass() {
System.out.println("beforeClass");
}
}
@AfterClass
public static void afterclass() {
System.out.println("afterClass");
}
}