I'm using cucumber-jvm
in my integration tests and I need to execute some code after all scenarios are finished, just once.
After reading carefully some posts like this and reviewed this reported issue, I've accomplished it doing something like this:
public class ContextSteps {
private static boolean initialized = false;
@cucumber.api.java.Before
public void setUp() throws Exception {
if (!initialized) {
// Init context. Run just once before first scenario starts
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// End context. Run just once after all scenarios are finished
}
});
initialized = true;
}
}
}
I think the context initialization (equivalent to BeforeAll
) done in this way is fine. However, although it's working, I'm not sure at all if the AfterAll
simulation using Runtime.getRuntime().addShutdownHook()
is a good practice.
So, these are my questions:
- Should I avoid
Runtime.getRuntime().addShutdownHook()
to implementAfterAll
? - Are there other better choices to emulate the
AfterAll
behaviour in cucumber-jvm?
Thanks in advance for the help.
See Question&Answers more detail:os