英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论