Using Hooks

Cucumber supports hooks, which are methods that run before or after each scenario. You can define them anywhere in your support or step definition layers, using the annotations @Before and @After.

To test them, add a file src/test/java/hooks/SomeTestHooks.java that looks like this:

support_code/13/src/test/java/hooks/SomeTestHooks.java
 
package​ hooks;
 
 
import​ cucumber.api.java.After;
 
import​ cucumber.api.java.Before;
 
import​ cucumber.api.Scenario;
 
 
public​ ​class​ SomeTestHooks {
 
@Before
 
public​ ​void​ beforeCallingScenario() {
 
System​.out.println(​"*********** About to start the scenario."​);
 
}
 
 
@After
 
public​ ​void​ afterRunningScenario(Scenario scenario) {
 
System​.out.println(​"*********** Just ...

Get The Cucumber for Java Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.