java 全局钩子,AfterAll全局钩子cucumber-jvm

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 implement AfterAll?

Are there other better choices to emulate the AfterAll behaviour in cucumber-jvm?

Thanks in advance for the help.

解决方案

Probably a better way would be to use a build tool, like Ant, Maven or Gradle for set-up and tear-down actions, which are part of integration tests.

When using Maven Fail Safe Plug-in, for setting up integration tests. There is the phase pre-integration-test, which is typically used for setting up the database and launch the web-container. Then the integration-tests are run (phase integration-test). And afterwards the phase post-integration-test is run, for shutting down and closing / removing / cleaning up things.

INFO In case the Cucumber tests are run through JUnit, the following might also be worth considering

In case it is simpler, smaller set up stuff, you can have a look at the JUnit @BeforeClass and @AfterClass. Or implement a JUnit @ClassRule, which has it's own before() and after() methods.

@ClassRule

public static ExternalResource resource = new ExternalResource() {

@Override

protected void before() throws Throwable {

myServer.connect();

}

@Override

protected void after() {

myServer.disconnect();

}

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值