java - Is calling close() on object returned by MockitoAnnotations.openMocks(Object) is mandatory?
问题描述
I would like is it mandatory to call close() on object returned by MockitoAnnotations.openMocks(Object)? If answer is yes, why do I need to call it? Assume we have such an example:
public class MockitoAnnotationsOpenMocksTest {
@Mock
private Foo var;
@BeforeMethod
public void before() {
MockitoAnnotations.openMocks(this);
}
@Test
public void foo() {
Mockito.when(var.name()).thenReturn("foo");
System.out.println("test foo" + ", var name " + var.name());
}
@Test
public void foo2() {
System.out.println("test foo2" + ", var name " + var.name());
}
private class Foo {
String name() {
return "";
}
}
}
running it with testng result is:
[RemoteTestNG] detected TestNG version 6.14.2
test foo, var name foo
test foo2, var name null
PASSED: foo
PASSED: foo2
PASSED: foo2
AFAIK in every call of @BeforeMethod MockitoAnnotations.openMocks(this); initializes var object one more time, so I do not see any sense to use close() method after every test method.
解决方案
The documentation says:
If static method mocks are used, it is required to close the initialization. Additionally, if using third-party mock-makers, other life-cycles might be handled by the open-release routine.
So no, tests won't fail under all circumstances if you don't close the mocks, but there are cases where it's necessary, so any generic test framework should make sure to close()
.
推荐阅读
- intel-mkl - 静态和动态集群 MKL 有什么区别?
- qt5 - 在 Qt 中,如何根据另一个模型的更改来更新模型的布局?
- reactjs - 访问状态时,WebStorm 自动完成功能不适用于 ReactJs
- c# - 允许 API json 上的反斜杠
- python - 在 gridsearchcv 中正确使用 CV
- google-sheets - 我做了一个计算器,想使用一个按钮将总和存储到另一个单元格以保存单元格
- javascript - 如何使用 contextAPI 在调度中使用等待?
- java - 如何打开默认数字助理设置?
- visual-studio-code - 在 VSCode 中配置工作区 - 需要完整的选项参考
- python - 按屏幕上的任意位置关闭弹出窗口