The ruby version of cucumber supports a global before hook. An *.rb file placed in the features/support directory gets apparently called only once, before any and all scenarios run. See https://github.com/cucumber/cucumber/wiki/Hooks#global-hooks
That seems to be a great way to make sure a database (used in read-only during tests) gets populated (and thus is in a known state) before any tests run.
Is there a similar feature available for the Java version of cucumber?
Almost all BDD automation frameworks have some sort of hooks that run before and after scenarios. However, not all frameworks have global hooks that run once at the beginning or end of a suite of scenarios – and Cucumber-JVM is one of these unlucky few.
What is Hook in Cucumber? In Cucumber, the hook is the block of code which can be defined with each scenario in step definition file by using the annotation @Before and @After. These @Before and @After annotations create a block in which we can write the code.
Apache Maven is the preferred build management tool for Cucumber-JVM projects. All Cucumber-JVM packages are available from the Maven Central Repository. Maven can automatically run Cucumber-JVM tests as part of the build process.
Unlike TestNG Annotations, cucumber supports only two hooks (Before & After) which works at the start and the end of the test scenario. As the name suggests, @before hook gets executed well before any other test scenario, and @after hook gets executed after executing the scenario.
There is no such feature natively in Cucumber JVM (see https://github.com/cucumber/cucumber-jvm/issues/515).
However, there are a couple of workarounds:
@BeforeAll
and @AfterAll
for JUnit 5, @BeforeClass
and @AfterClass
for JUnit 4Before
hook for lazy singleton initialization and a JVM shutdown hook for teardownEventListener
and subscribe to TestRunStarted
and TestRunFinished
eventspre-integration-test
, integration-test
, post-integration-test
phases and the maven-failsafe-plugin
.You will also have to solve the issue of injecting the results of such a setup step (e.g. random port numbers) into your tests.
I wrote a blog article to cover all details: https://metamorphant.de/blog/posts/2020-03-10-beforeall-afterall-cucumber-jvm-junit/
Please follow this link. this a workaround for @BeforeAll and @AfterAll
https://github.com/cucumber/cucumber-jvm/issues/515
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With