0
votes

applicationContext

<bean id="contentRegisteringBean" parent="abstractRegisteringBean" lazy-init="false">
    <property name="processor">
        <bean class="com.somepackage.ContentService$Processor"/>
    </property>
</bean>

<bean id="abstractRegisteringBean" class="test.spring.MockFactoryBean">
    <property name="type" value="com.somepackage.ProcessorRegisteringBeanImpl"/>
</bean>

ProcessorRegisteringBeanImpl

public class ProcessorRegisteringBeanImpl {
    private Processor mProcessor;


    public Processor getProcessor() {
        return mProcessor;
    }

    public void setProcessor(final Processor processor) {
        mProcessor = processor;
    }
}

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;
    }

}

Exception:

org.springframework.beans.NotWritablePropertyException: Invalid property 'processor' of bean class [test.spring.MockFactoryBean]: Bean property 'processor' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

1
What is MockFactoryBean?axtavt
@axtavt look at updated question plsVB_

1 Answers

2
votes

When you declare a FactoryBean, Spring expects that you configure properties of the FactoryBean rather than properties of the object it creates.

Try the following instead:

<bean id="contentRegisteringBean" parent="abstractRegisteringBean" lazy-init="false">
    <!-- Define concrete class to pass to Mockito.mock() -->
    <constructor-arg value = "com.somepackage.ProcessorRegisteringBeanImpl" />

    <property name="processor">
        <bean class="com.somepackage.ContentService$Processor"/>
    </property>
</bean>

<!-- Abstract definition of beans created using Mockito.mock() -->
<bean id="abstractRegisteringBean" abstract = "true"
     class="org.mockito.Mockito" factory-method = "mock">
</bean>