Search code examples
junitcucumber

Annotation for method, which must work before a group of features (Cucumber + Junit)


Cucumber supports hooks -- methods that run before or after a scenario. The @Before and @After annotations are used to mark them. A method with @Before annotation will run before each scenario, @After -- after each scenario. An example of a class with hooks:

public class Hooks {

  @Before
  public void init() {
    System.out.println("before each Cucumber scenario");
  }

  @After
  public void stop() {
    System.out.println("after each Cucumber scenario");
  }
}

Can you tell me, please, what annotations I must use in order to run method 1 time before the entire group of Cucumber-scenarios (feature-files)?

If there is no such annotation, then how can we do it in another way?


Solution

  • You can use the standard Junit annotation @BeforeAll and @AfterAll

    @BeforeAll methods are only executed once for a given test class. @BeforeAll is used to signal that the annotated method should be executed before all tests in the current test class.

    Please refer this documentation @BeforeAll