将模拟bean注入到spring上下文中进行测试
我知道有人问过类似的问题,例如这里,但已经完成搜索后,我找到了一个解决方案,我对 更满意然而
,我唯一的问题是我不确定如何实现这个解决方案。
我想要做的是通过 HotswappableTargetSource 用我的测试版本覆盖我的应用程序上下文中选择的 bean 的 bean 定义,然后运行测试。
然后,对于每个测试用例,我想指定哪些 bean 是我想要热插拔的,然后每个测试必须能够创建自己的模拟版本并交换它们,并且能够再次交换回来。
我能够获取运行测试的应用程序上下文,但我不知道如何将 bean 配置为可热插拔。我知道在使用 xml 配置 beans 时该怎么做,但我不想回到使用 xml 来配置 beans。
I know similar questions have been asked, e.g. here, but having done a search, I've come upon a solution I'm much happier with here
My only problem however, is that I'm not sure how to implement this solution.
What I want to be able to do is via the HotswappableTargetSource override the bean definitions of select beans in my application context with my test versions and then run the test.
Then for each test case I'd like to specify which beans I want to be hot swappable and then each test must be able to create its own mock versions and swap those in, and be able to swap back again.
I am able to get the Application Context the test is running with but what I don't know is how to configure a bean to be hot swappable. I know how to do it when configuring beans with xml but I don't want to go back to using xml to configure beans.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:有一个库可以做到这一点!
https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations
解决方案如下:
您需要更改应用程序的 spring 上下文来代理您想要交换的 bean:
因此需要对被测系统进行更改。
在您的测试中,代码将如下所示:
UPDATE: There's a library that does it!
https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations
The solution is as follows:
You will need to change the spring context of your application to proxy the bean you want to swap:
Thus a change to the system under test is necessary.
And in your test the code will look like: