Spring 3.2
The problem is this, you need to write a test for the rest services level and lock the whole level of managers, otherwise the context simply will not rise. Because spring will say that there is no such bean. That is, let's say in test-context.xml for each such manager I have to declare
<bean id="manager1" class="org.easymock.EasyMock" factory-method="createMock"> constructor-arg index="0" type="java.lang.Class" value="aaManager1" /> </bean> but the problem is that there are about a hundred such managers, and it turns out that I have to manually create the mocks of these bins.
I still thought of creating MockFactory
public class EasyMockBeanFactory implements FactoryBean { private Class type; @Override public Object getObject() throws Exception { return EasyMock.createNiceMock(type); } @Override public Class<?> getObjectType() { return type; } @Override public boolean isSingleton() { return true; } public Class getType() { return type; } public void setType(Class type) { this.type = type; } } which creates me a mock class that I pass on.
We also managed to extend DefaultListableBeanFactory
public class DefaultMockBeanFactory extends DefaultListableBeanFactory { @Override public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException { return super.getBeanDefinition(beanName); } @Override protected <T> T doGetBean(String name, Class<T> requiredType, Object[] args, boolean typeCheckOnly) throws BeansException { if (super.containsBeanDefinition(name)) { return super.doGetBean(name, requiredType, args, typeCheckOnly); } logger.info("need mock: " + name); registerBeanDefinition(name, createDef(requiredType)); return super.doGetBean(name, requiredType, args, typeCheckOnly); } private RootBeanDefinition createDef(Class type) { RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(EasyMockBeanFactory.class); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("type", type); rootBeanDefinition.setPropertyValues(propertyValues); return rootBeanDefinition; } } but the problem is that in this method the Class requiredType is always null, so I can’t find out the type of this class to create a mock out of it.
Is it possible in Spring to indicate that if there is no any bean, then create it using MockFactory. Either override any bin scanner, which already after generating the name of the bin (which will be used as a dependency), will save the candidate's name for the bin and its type, in order to use it in DefaultListableBeanFactory.
The question is of course a little addict. There may be other ways to achieve this goal.