更新:有一个图书馆能做到!
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
}
}