Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Unsatisfied dependency expressed through bean property & NoSuchBeanDefinitionException

Posted on 2009-02-24
7
Medium Priority
?
6,281 Views
Last Modified: 2012-05-06
I'm running JUnit Test Cases which are spring based. When I run a test case, I'm getting below exception:
Error creating bean with name 'com.xyz.TestActionConvertor': Unsatisfied dependency expressed through bean property 'dataSource': Set this property value or disable dependency checking for this bean.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.xyz.TestActionConvertor': Unsatisfied dependency expressed through bean property 'dataSource': Set this property value or disable dependency checking for this bean.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:1039)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:858)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:274)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:179)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:158)
at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:88)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)



dataSource is defined in spring_context.xml as below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 
        <!-- Datasource -->
    <bean id="dataSource" class="be.isabel.pdg.common.db.BasicDatasource">
        <property name="driverClassName"><value>${bank.jdbc.driverclassname}</value></property>
        <property name="url"><value>${bank.jdbc.url}</value></property>
        <property name="password"><value>${bank.jdbc.password}</value></property>
        <property name="username"><value>${bank.jdbc.username}</value>
        </property>
    </bean>
   
        <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
                <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.c3p0.max_size">${bank.hibernate.c3p0.max_size}</prop>
                <prop key="hibernate.c3p0.min_size">${bank.hibernate.c3p0.min_size}</prop>
                <prop key="hibernate.c3p0.timeout">${bank.hibernate.c3p0.timeout}</prop>
                <prop key="hibernate.dialect">${bank.hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${bank.hibernate.show_sql}</prop>
            </props>
        </property>
        <property name="lobHandler"><ref bean="lobHandler"/></property>
    </bean>
</beans>
 

Also, I'm getting below exception for some other TestCases:


Exception in constructor: testGetCompanyNumbersForBankUserID (org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'bankUserRoutingDAOBean' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:360) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:914) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:241) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:757) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:494) at junit.framework.TestSuite.createTest(TestSuite.java:131) at junit.framework.TestSuite.addTestMethod(TestSuite.java:114) at junit.framework.TestSuite.<init>(TestSuite.java:75) at org.junit.internal.runners.JUnit38ClassRunner.<init>(JUnit38ClassRunner.java:69) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:494) at org.junit.internal.requests.ClassRequest.buildRunner(ClassRequest.java:33) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:28) at junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:28) at junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:21) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:494) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:387) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:912) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:743) )


Please let me know if further info needed and let me know where its going wrong?
0
Comment
Question by:raghugowda
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:mahome
ID: 23721799
Can you post your TestClass

You need to extend AbstractTransactionalDataSourceSpringContextTests and define getConfigLocations()
0
 

Author Comment

by:raghugowda
ID: 23721972
I have got both as below:
public class TestActionConvertor extends PBPersistTestCase {
 
	public void testToDO() {
		//code
	}
 
	public void testToDTO() {
		//code
	}
 
public class PBPersistTestCase extends PBTestCase {
	public static final String SPRING_MYSQL_CONTEXT_FILES = "common_spring_context.xml;"
					+ "junit_persistance_spring_context.xml;"
						+ "persistance_spring_context.xml";
	
		
	protected String[] getConfigLocations() {
		System.out.println(SPRING_MYSQL_CONTEXT_FILES.split(";"));
		return SPRING_MYSQL_CONTEXT_FILES.split(";");
	}
 
	public PBPersistTestCase() {
		super();
	}
 
	public PBPersistTestCase(String testName) {
		super(testName);
	}
 
	protected String getSpringContextFiles() {
		return SPRING_MYSQL_CONTEXT_FILES;
	}
}
 
PBTestCase extends ABCTestCase{.......}
 
ABCTestCase extends AbstractTransactionalDataSourceSpringContextTests{.......}

Open in new window

0
 
LVL 10

Expert Comment

by:mahome
ID: 23722206
If you have no additional config for testing add    
setAutowireMode(AUTOWIRE_BY_NAME) as follows.

 protected String[] getConfigLocations() {
                setAutowireMode(AUTOWIRE_BY_NAME);
                System.out.println(SPRING_MYSQL_CONTEXT_FILES.split(";"));
                return SPRING_MYSQL_CONTEXT_FILES.split(";");
        }

Open in new window

0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 

Author Comment

by:raghugowda
ID: 23722489
I tried as you said, but still same error: Error creating bean with name ..............
0
 
LVL 10

Expert Comment

by:mahome
ID: 23722577
above you mentioned datasource is defined in spring_context.xml, is it also defined in one of those file: "common_spring_context.xml;" + "junit_persistance_spring_context.xml;"                                            + "persistance_spring_context.xml

and with the name "dataSource" (be careful of Case), you could also try AUTOWIRE_BY_TYPE, but if you've named your bean correct that should make no difference
0
 

Author Comment

by:raghugowda
ID: 23722684
I had renamed junit_persistance_spring_context.xml to spring_context.xml while posting, it contains "dataSource "defined.

 I have checked the case, its correct "dataSource".

In persistance_spring_context.xml & common_spring_context.xml I have defined other beans like:
<bean id="addressedDocRefDAOBean" parent="abc" class="xyz"/>
0
 

Accepted Solution

by:
raghugowda earned 0 total points
ID: 23772723
I got to know that problem was due to incorrect path to certain properties files where all configuration resides, so it was not able to find property and definition of bean.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question