O'Reilly logo

The Cucumber for Java Book by Seb Rose, Matt Wynne, Aslak Hellesoy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required