This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

Problem with AbstractTransactionalDataSourceSpringContextTesPage Title Module

org.springframework.beans.factory.UnsatisfiedDependencyException&#58; Error creating bean with name 'com.vanitysoft.griffin.service.rss.impl.RSSServiceTest' defined in null&#58; Unsatisfied dependency expressed through bean property 'rssService'&#58; There are 4 beans of type &#91;interface com.vanitysoft.griffin.service.rss.IRSSService&#93; for autowire by type. There should have been 1 to be able to autowire property 'rssService' of bean 'com.vanitysoft.griffin.service.rss.impl.RSSServiceTest'.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType&#40;AbstractAutowireCapableBeanFactory.java&#58;800&#41;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean&#40;AbstractAutowireCapableBeanFactory.java&#58;720&#41;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties&#40;AbstractAutowireCapableBeanFactory.java&#58;188&#41;
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp&#40;AbstractDependencyInjectionSpringContextTests.java&#58;141&#41;
at junit.framework.TestCase.runBare&#40;TestCase.java&#58;125&#41;
at junit.framework.TestResult$1.protect&#40;TestResult.java&#58;106&#41;
at junit.framework.TestResult.runProtected&#40;TestResult.java&#58;124&#41;
at junit.framework.TestResult.run&#40;TestResult.java&#58;109&#41;
at junit.framework.TestCase.run&#40;TestCase.java&#58;118&#41;
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests&#40;RemoteTestRunner.java&#58;478&#41;
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run&#40;RemoteTestRunner.java&#58;344&#41;
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main&#40;RemoteTestRunner.java&#58;196&#41;

By default the unit test context support tries to do autowiring by type. Read the reference docs, javadocs and maybe take a look at the source to see how you bypass this.
(btw, the app context you showed doesn't contain the RSSServiceTest)

Comment

You're getting a complaint about there being 4 instances of the IRSSService interface. Now I'm not sure you're showing your entire code, as I only see 2 instances (unless I'm missing something), but even two instances is one two many for this use case.

AbstractDependencyInjectionSpringContextTests and its subclasses by default inject by type, so if there is more than one bean of that type you're going to get the exception you got. There are a few different ways to work around this:

1: if you just have two instances, and one is an outer proxy (like a transactional wrapper) and one is a target object, then as long as nobody needs the unwrapped target object (which is usually the case), then you can just make the target an inner bean of the wrapper, effectively hiding it from all byType autowiring. This is usually the easiest way to handle this problem when there is only one wrapper and one target.

2: you can switch to by name autowiring of the test. In this case, you set the populateProtectedVariables property of the base test class to true, and it will autowire protected fields by _name_.

3: you can manually get the problem bean yourself. Implement the onSetup() template method, and call

Code:

applicationContext.getBean&#40;"xxxx"&#41;

to get the actual bean you need and set it as a field in your test case class.

Regards,

Comment

can you fix this to your solution u stated:
1: if you just have two instances, and one is an outer proxy (like a transactional wrapper) and one is a target object, then as long as nobody needs the unwrapped target object (which is usually the case), then you can just make the target an inner bean of the wrapper, effectively hiding it from all byType autowiring. This is usually the easiest way to handle this problem when there is only one wrapper and one target.