在@Before方法中重新连接Spring bean [英] Re-wire spring bean in @Before method

查看:84
本文介绍了在@Before方法中重新连接Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JUnit对@Controller注释的类进行单元测试.

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

Spring是否可以将控制器重置(重新布线")为其默认注入状态? 构建一个全新的控制器对象将没有问题.

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.

我发现的解决方法是将构造函数的可见性设置为protected,并在@Before注释的类的开头执行ControllerObj controllerObj = new ControllerObj(),但是我显然不想仅出于测试目的而增加构造函数的可见性.

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.

推荐答案

根据 @ mate00 的建议,解决方案是使用@DirtiesContext注释.

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

这篇关于在@Before方法中重新连接Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆