I have: beans like
<bean id="abstractBean" class="com.package.MyBean" abstract="true"/>
<bean id="heirBean" parent="abstractBean">
<property name="someProperty" ref="anotherBean">
</bean>
Question: How to mock heirBean
? Or in other words how to mock abstractBean
?
======================== OPTIONAL PART OF QUESTION ==============================
How do I tried to do that [with exception]:
<bean id="abstractBean" class="MockFactoryBean">
<property name="type" value="com.package.MyBean"/>
</bean>
MockFactoryBean.java
public class MockFactoryBean<T> implements FactoryBean<T> {
private Class<T> type;
public void setType(Class<T> type) {
this.type = type;
}
@Override
public T getObject() throws Exception {
return Mockito.mock(type);
}
@Override
public Class<T> getObjectType() {
return type;
}
@Override
public boolean isSingleton() {
return true;
}
}
Problem: I can't set fields of mock.
有一个框架可让您向Spring xml文件添加模拟功能-https: //bitbucket.org/kubek2k/springockito/wiki/Home
Generally, you don't mock parent beans, you mock each bean you want to mock.
As I understand your problem:
While the theory generally discourages refactoring for testing, the specific needs of the tests allow you to see the ways you could refactor your code.
I'd propose:
Therefore you replace inheritance with usage , which allows you much more flexibility.
I would use the profile concept of spring; from spring 3.2.X there is the "profile" concept in Spring and you can use it to mock your bean for test scope
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.