Property 'sampleService' is required for bean 'test.ThohapiTests'

Posted on 02/06/2009 by Charalampos Chrysikopoulos

Another spring configuration propeblem:

I have a test case I want to run using a specific configuration file. The test case looks like this:

<br />@ContextConfiguration(locations = {"classpath:system-test-config.xml"})<br />@RunWith(SpringJUnit4ClassRunner.class)<br />public class ThohapiTests {<br /><br />private final static Logger logger = Logger.getLogger(ThohapiTests.class);<br /><br />@Resource(name="sampleService")<br />private SampleService service;<br /><br />@Test<br />public void testSampleService() {<br /><br /> Worker result1 = service.storeWorker();<br /> logger.debug(result1);<br /> assertNotNull(result1);<br /><br /> Worker result2 = service.storeWorker();<br /> logger.debug(result2);<br /> assertNotNull(result2);<br /><br /> logger.debug(service.getWorker(result1.getId()));<br /> logger.debug(service.getWorker(result2.getId()));<br />}<br /><br />@Required<br />public void setSampleService(SampleService sampleService) {<br /> this.service = sampleService;<br />}<br />}<br />

My configuration file, where I define my sampleService is this:

<br /><br /><beans xmlns="http://www.springframework.org/schema/beans" xsi="http://www.w3.org/2001/XMLSchema-instance" context="http://www.springframework.org/schema/context" aop="http://www.springframework.org/schema/aop" schemalocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-2.5.xsd            http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><br /><br /><import resource="classpath:application-config.xml"><br /><br /><aop:aspectj-autoproxy class="true"><br /><context:annotation-config><br /><br />   <bean id="pmf" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean"><br />       <property name="jdoProperties"><br />           <props><br />               <prop key="javax.jdo.PersistenceManagerFactoryClass">org.datanucleus.jdo.JDOPersistenceManagerFactory</prop><br />               <prop key="javax.jdo.option.ConnectionURL">jdbc:hsqldb:mem:dbname</prop><br />               <prop key="javax.jdo.option.ConnectionUserName">sa</prop><br />               <prop key="javax.jdo.option.ConnectionPassword"></prop><br />               <prop key="javax.jdo.option.ConnectionDriverName">org.hsqldb.jdbcDriver</prop><br />              <br />               <prop key="datanucleus.autoCreateTables">true</prop><br />           </props><br />       </property><br />   </bean><br /><br />   <bean id="jdoTransactionManager" class="org.springframework.orm.jdo.JdoTransactionManager"><br />       <property name="persistenceManagerFactory"><ref local="pmf"></ref><br />   </property><br /><br />   <bean id="sampleService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><br />    <qualifier value="sampleServie"><br />       <property name="transactionManager" ref="jdoTransactionManager"><br />       <property name="target" ref="sampleServiceImpl"><br />       <property name="transactionAttributes"><br />           <props><br />               <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop><br />               <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop><br />               <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop><br />               <prop key="store*">PROPAGATION_REQUIRED</prop><br />               <prop key="delete*">PROPAGATION_REQUIRED</prop><br />           </props><br />       </property><br />   </property><br /></property><br /></qualifier></bean></bean></context:annotation-config></aop:aspectj-autoproxy></import></beans>
>

and the business beans are defined in:

<br /><br /><beans xmlns="http://www.springframework.org/schema/beans" xsi="http://www.w3.org/2001/XMLSchema-instance" context="http://www.springframework.org/schema/context" schemalocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-2.5.xsd"><br /><br /><bean id="sampleDAOImpl" class="gr.open.thohapi.daos.SampleDAOImpl"><br /> <property name="persistenceManagerFactory" ref="pmf"><br /></property><br /><br /><bean id="toolDAOImpl" class="gr.open.thohapi.daos.ToolDAOImpl"><br /> <property name="persistenceManagerFactory" ref="pmf"><br /></property><br /><br /><bean id="sampleServiceImpl" class="gr.open.thohapi.services.SampleServiceImpl"><br /> <property name="sampleDAO" ref="sampleDAOImpl"><br /> <property name="toolDAO" ref="toolDAOImpl"><br /></property><br /><br /></property><br /></bean></bean></bean></beans>

Running the test case, produces the following error:
org.springframework.beans.factory.BeanInitializationException: Property 'sampleService' is required for bean 'test.ThohapiTests'
at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:121)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:998)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:93)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:130)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Solution:
Deleting the @Required annotation from the setter of the sampleService makes everything work fine.

This entry was posted in configuration, spring and tagged by Charalampos Chrysikopoulos