Re-wire spring bean in @Before method

杀马特。学长 韩版系。学妹 提交于 2020-05-17 06:34:33

问题


I want to unit test an @Controller-annotated class, using JUnit.

Is it possible for Spring to reset ("re-wire") the controller to its default injected state ? There would be no problem building a all new controller object.

A workaround I found is to set the constructor's visibility to protected and execute ControllerObj controllerObj = new ControllerObj() at the beginning of @Before-annotated class but I obviously do not want to increase contructor's visibility just for testing purpose.


回答1:


As suggested by @mate00 the solution was to use the @DirtiesContext annotation.



来源:https://stackoverflow.com/questions/58390044/re-wire-spring-bean-in-before-method

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