spring Should I use @DirtiesContext on every class

不问归期 提交于 2019-12-02 17:37:58

Spring caches the application context by default when running tests. The key that Spring uses for the cache is made of the following:

  • locations (from @ContextConfiguration)
  • classes (from @ContextConfiguration)
  • contextInitializerClasses (from @ContextConfiguration)
  • contextLoader (from @ContextConfiguration)
  • activeProfiles (from @ActiveProfiles)
  • resourceBasePath (from @WebAppConfiguration)

All the details of the caching can be found in the documentation.

In my experience, there is rarely a need to use @DirtiesContext in order to force Spring to recreate the context. I haven't come across too many situations where it's needed - the only one that comes to mind easily is the use of a shared cache manager.

You are better using it only on tests that you absolutely positively need it. Execution speed will be far too slow if you use @DirtiesContext on every test and you won't be getting anything in return.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!