Another spring configuration propeblem:

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


@ContextConfiguration(locations = {"classpath:system-test-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class ThohapiTests {

private final static Logger logger = Logger.getLogger(ThohapiTests.class);

@Resource(name="sampleService")
private SampleService service;

@Test
public void testSampleService() {

Worker result1 = service.storeWorker();
logger.debug(result1);
assertNotNull(result1);

Worker result2 = service.storeWorker();
logger.debug(result2);
assertNotNull(result2);

logger.debug(service.getWorker(result1.getId()));
logger.debug(service.getWorker(result2.getId()));
}

@Required
public void setSampleService(SampleService sampleService) {
this.service = sampleService;
}
}


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












org.datanucleus.jdo.JDOPersistenceManagerFactory
jdbc:hsqldb:mem:dbname
sa

org.hsqldb.jdbcDriver

true














PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED






and the business beans are defined in:




















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.

0 comments: