确保在Cucumber步骤定义中关闭Autocloseable。

huangapple go评论69阅读模式
英文:

Ensure Autocloseable is closed in Cucumber step definitions

问题

我的Cucumber Java步骤定义可以创建AutoCloseable对象,这些对象可以获取外部(可能是稀缺的)资源。当这些对象不再需要时,应调用它们的close()方法以释放这些资源。如何确保调用close()方法?

在非Cucumber测试中,我会使用try-with-resources。但由于没有一个单一的方法跨越场景的执行(一个AutoCloseable对象可能在when方法中创建,并在稍后的then中使用),这是不可能的。

英文:

My Cucumber Java step definitions can create AutoCloseable objects, which can acquire external (perhaps scarce) resources. Those objects should have their close() method called when they are no longer needed, to free those resources. How do I ensure that the close() methods are called?

In a non-Cucumber test, I'd use try-with-resources. But that is not possible because there is no single method that spans the execution of a scenario (an AutoCloseable object might be created in a when method, and used in a later then).

答案1

得分: 1

"一种选择是使用带有 io.cucumber.java.After 注释的方法来调用 close() 方法(们)。这并不是一个很好的选择;我们必须依赖于Cucumber,而不是Java本身,来执行正确的操作。"

英文:

One option is to use a method annotated with io.cucumber.java.After to callthe close() method(s). It's not great; we have to rely on Cucmber, rather than Java itelf, doing the right thing.

huangapple
  • 本文由 发表于 2020年8月13日 22:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定