使用 spring 测试类时模拟配置值
我正在尝试测试一个像这样的类,
@Controller
public class FailureController {
@Value("#{configValues.defaultRedirectUrl}")
private String defaultRedirectUrl;
public FailureController (String defaultRedirectUrl) {
this.defaultRedirectUrl = defaultRedirectUrl;
}
...
问题是如果不为测试类创建一个特殊的构造函数(如上所示),我就无法测试这个类,这最终会初始化 中的
defaultRedirectUrl
失败控制器。
在测试期间如何在不创建构造函数(来自 spring 上下文)的情况下测试它。我的主要目标是在运行测试时在没有构造函数的情况下初始化 FailureController 中的值。
是否有可能以某种方式在测试期间加载 spring 上下文并初始化 FailureController 中的字段 这就是我一直在做的事情,但它不起作用,defaultRedirectUrl
在FailureController
中仍然是null
。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext-test.xml" })
public class FailureControllerTest {
@Autowired
FailureController failureController;
applicationContext-test.xml
...
<bean id="failureController" class="se.synergica.watchtower.controllers.FailureController">
</bean>
<import resource="spring-config-test.xml" />
</beans>
谢谢。 阿尔
I am trying to test a class like
@Controller
public class FailureController {
@Value("#{configValues.defaultRedirectUrl}")
private String defaultRedirectUrl;
public FailureController (String defaultRedirectUrl) {
this.defaultRedirectUrl = defaultRedirectUrl;
}
...
The problem is that I can't test this class without creating a special constructor for test class (like above), which is eventually to initializes the defaultRedirectUrl
in FailureController
.
How can I test it without creating a constructor (that are to be come from spring context) during the test. My main objective is to initialize the values in FailureController
without a constructor when running a test.
Is it possible in some way that spring context gets loaded during the test and initializes the fields in FailureController
this is what i have been doing but its not working, defaultRedirectUrl
remains null
in FailureController
.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext-test.xml" })
public class FailureControllerTest {
@Autowired
FailureController failureController;
applicationContext-test.xml
...
<bean id="failureController" class="se.synergica.watchtower.controllers.FailureController">
</bean>
<import resource="spring-config-test.xml" />
</beans>
thank you.
al
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将在您的测试类路径(标准项目中的 src/test/resources)中创建一个单独的 applicationContext.xml 文件。在那里我将设置一个带有虚拟值的 configValues 对象。
您的被测类也应该在该上下文中实例化。
然后在 junit 测试中加载这样的测试上下文:
有关 spring 测试的更多信息: http://static.springsource.org/spring/docs/3.0.x/reference/testing.html
I would create a separate applicationContext.xml file in your test-classpath (src/test/resources in standard projects). There I would set up a configValues object with dummy values.
Your class under test should also be instanciated in that context.
Then load the test context like this in a junit test:
more on spring testing: http://static.springsource.org/spring/docs/3.0.x/reference/testing.html
这些配置属性通常来自 .properties 文件 - 只需在单元测试类路径中使用不同的属性文件(带有测试值)
These config properties usually come from .properties files - simply use a different properties file (with test values) in the unit test classpath