将模拟bean注入到spring上下文中进行测试

发布于 2024-10-04 14:16:13 字数 559 浏览 7 评论 0原文

我知道有人问过类似的问题,例如这里,但已经完成搜索后,我找到了一个解决方案,我对 更满意然而

,我唯一的问题是我不确定如何实现这个解决方案。

我想要做的是通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2024-10-11 14:16:13

更新:有一个库可以做到这一点!

https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations

解决方案如下:

您需要更改应用程序的 spring 上下文来代理您想要交换的 bean:

<bean id="beanSwappable" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="targetSource" ref="beanSwap" />
</bean>

<bean id="beanSwap" class="org.springframework.aop.target.HotSwappableTargetSource">
    <constructor-arg ref="beanToSwap" />
</bean>
  • beanSwap 是此 beanSwap 的代理。
  • beanSwappable 是当您想要交换 bean 时引用的 bean
  • beanToSwap 是 bean 的默认实现,

因此需要对被测系统进行更改。

在您的测试中,代码将如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "test.xml", "spring.xml" })
public class Test {

    @Resource(name="beanSwappable")
    Bean b;

    @Resource(name = "beanSwap")
    HotSwappableTargetSource beanSwap;

    public void swap() {
        Bean b = << create mock version >>
        beanSwap.swap(b);
        // run test code which

    }
}

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:

<bean id="beanSwappable" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="targetSource" ref="beanSwap" />
</bean>

<bean id="beanSwap" class="org.springframework.aop.target.HotSwappableTargetSource">
    <constructor-arg ref="beanToSwap" />
</bean>
  • beanSwap is the proxy onto this beanSwap.
  • beanSwappable is the bean which you reference when you want to swap the bean
  • beanToSwap is the default implementation of the bean

Thus a change to the system under test is necessary.

And in your test the code will look like:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "test.xml", "spring.xml" })
public class Test {

    @Resource(name="beanSwappable")
    Bean b;

    @Resource(name = "beanSwap")
    HotSwappableTargetSource beanSwap;

    public void swap() {
        Bean b = << create mock version >>
        beanSwap.swap(b);
        // run test code which

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文