我想继续this提问。
但是对于bar示例,spring并不推断从泛型mock()方法返回的bean的类型。但实际上它与MockFactoryBean情况完全相同,即可行。
class MockFactoryBean<T> implements FactoryBean<T> {
private Class<T> type;
public void setType(final Class<T> type) {
this.type = type;
}
@Override
public T getObject() throws Exception {
return (T) Mockito.mock(type);
}
@Override
public Class<T> getObjectType() {
return type;
}
@Override
public boolean isSingleton() {
return true;
}
}
加
<bean id="mockFactory" name="someType" class="com.example.MocksFactory" >
<property name="type" value="com.example.SomeType" />
</bean>
但使用org.mockito.Mockito
和mock()
工厂方法无法正常工作
<bean id="dao" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.example.SomeType" />
</bean>
并且当自动装配时,一个得到[com.example.SomeType]类型的匹配bean ....
@Autowired public SomeType someType;
为什么?
使用MockFactoryBean的方式非常不方便,因为如果你必须模拟很多bean,你最终会这样:
<bean id="mockFactory1" name="metadataExtractor" class="tests.base.MocksFactory" >
<property name="type" value="processing.MetadataExtractor" />
</bean>
<bean id="mockFactory2" name="fileValidator" class="tests.base.MocksFactory" >
<property name="type" value="validation.file.FileValidator" />
</bean>
<bean id="mockFactory3" name="documentMatcher" class="tests.base.MocksFactory" >
<property name="type" value="validation.matching.DocumentMatcher" />
</bean>
<bean id="mockFactory4" name="uploadMatcher" class="tests.base.MocksFactory">
<property name="type" value="validation.matching.UploadMatcher" />
</bean>
<bean id="mockFactory5" name="tempFileLocalService" class="tests.base.MocksFactory">
<property name="type" value="service.TempFileLocalService" />
</bean>
<bean id="mockFactory6" name="orderLocalService" class="tests.base.MocksFactory">
<property name="type" value="service.OrderLocalService" />
</bean>
<bean id="mockFactory7" name="counterLocalService" class="tests.base.MocksFactory">
<property name="type" value="service.CounterLocalService" />
</bean>
因为没有id&amp;名称它不推断类型。
答案 0 :(得分:7)
当XML文件适用于标准spring / mockito jar时,您可以使用ProxyFactoryBean,它可以使用自动装配(在Spring 2.5上测试)。
<bean id="dao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target"> <bean class="org.mockito.Mockito" factory-method="mock"> <constructor-arg value="com.package.Dao" /> </bean> </property>
<property name="proxyInterfaces"> <value>com.package.Dao</value> </property>
</bean>
答案 1 :(得分:0)
我更进一步,在FactoryBean中添加了更多代码,并在我的测试类中保留真正的模拟创建。相反,我的FactoryBean在Spring启动时根据需要生成动态代理 - 这不会遇到您所看到的打字问题 - 并且测试可以根据需要连接到模拟中。我写了一篇blog post来描述如何做到这一点。这篇文章专门讨论了测试JAX-RS资源类的问题,但last section可以作为一种替代方式来做你正在尝试的东西,如果你模糊了JAX-RS的东西。
答案 2 :(得分:0)
我想出了什么:
public class SpringMocks implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(SpringMocks.class);
private final List<Class<?>> classes;
public SpringMocks(List<Class<?>> classes) {
this.classes = classes;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (applicationContext instanceof GenericApplicationContext) {
GenericApplicationContext context = (GenericApplicationContext) applicationContext;
DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory();
for (Class<?> className : classes) {
String beanName = RandomStringUtils.randomAlphanumeric(10);
Object singletonObject = Mockito.mock(className);
beanFactory.registerSingleton(beanName, singletonObject);
}
} else {
logger.warn("unable to add beans to the context!");
}
}
}
定义:
<bean class="com.whatever.SpringMocks">
<constructor-arg index="0">
<list>
<value>com.whatever.enricher.Enricher</value>
<value>com.whatever.nimbus.NimbusOrderDao</value>
<value>com.whatever.nimbus.NimbusAllocationDao</value>
<value>com.whatever.nimbus.NimbusExecutionDao</value>
<value>com.whatever.nimbus.NimbusBookingInstructionDao</value>
<value>com.whatever.services.SettingsService</value>
<value>com.whatever.matchers.utils.CVDataRetriever</value>
</list>
</constructor-arg>
</bean>
希望它能帮助那些正在寻找解决方案的人