测试如何“脏”? Spring应用程序上下文?
Spring 框架文档状态:
在不太可能的情况下,测试可能会 “脏”应用程序上下文, 需要重新加载 - 例如,通过 更改 bean 定义或 应用程序对象的状态 - Spring的测试支持提供 引起测试夹具的机制 重新加载配置并 之前重建应用程序上下文 执行下一个测试。
有人可以详细说明一下吗?我只是不明白。例子会很好。
The spring framework documentation states:
In the unlikely case that a test may
'dirty' the application context,
requiring reloading - for example, by
changing a bean definition or the
state of an application object -
Spring's testing support provides
mechanisms to cause the test fixture
to reload the configurations and
rebuild the application context before
executing the next test.
Can someone elaborate this? I am just not getting it. Examples would be nice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个 JUnit 测试方法都假定是隔离的,即不具有任何可能导致另一个测试方法表现不同的副作用。这可以通过修改 spring 管理的 bean 的状态来实现。
例如,假设您有一个由
MySpringBean
类的 spring 管理的 bean,它有一个值为"string"
的字符串属性。以下测试方法testBeanString
将产生不同的结果,具体取决于它是在方法testModify
之前还是之后调用。使用
@DirtiesContext
注解表明测试方法可能会改变spring管理的bean的状态。Each JUnit test method is assumed to be isolated, that is does not have any side effects that could cause another test method to behave differently. This can be achieved by modifying the state of beans that are managed by spring.
For example, say you have a bean managed by spring of class
MySpringBean
which has a string property with a value of"string"
. The following test methodtestBeanString
will have a different result depending if it is called before or after the methodtestModify
.use the
@DirtiesContext
annotation to indicate that the test method may change the state of spring managed beans.