# JUnit mit Spring und Hibernate



## y0dA (5. Nov 2009)

Hallo!

Ich versuche grade ein kleines Testprojekt mit Hibernate (mit Annotations) und Spring sowie einer Postgres Datenbank zu testen, leider bekomme ich immer folgende Exception:

```
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'at.gv.brz.dao.impl.AdresseDaoTest': Unsatisfied dependency expressed through bean property 'adresseDao': Set this property value or disable dependency checking for this bean.
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:1184)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1006)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:205)
	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:180)
	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:100)
	at junit.framework.TestCase.runBare(TestCase.java:132)
	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
```

meine Testklasse, welche von "AbstractTransactionalDataSourceSpringContextTests" ableitet - damit dachte ich eigentlich dass ich keine Dependency Probleme beim Test habe..


```
package at.gv.brz.dao.impl;

import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

import at.gv.brz.domain.Adresse;

public class AdresseDaoTest extends
		AbstractTransactionalDataSourceSpringContextTests {
	private AdresseDao adresseDao;

	@Override
	protected void onSetUpInTransaction() throws Exception {
	}

	public void testSpeicherAdresse() {
		Adresse adresse = new Adresse("Ort123", 3214, "Hauptstrasse 1337");
		Integer id = null;
		id = this.adresseDao.speicherAdresse(adresse);
		assertNotNull(id);
	}

	public void setAdresseDao(AdresseDao adresseDao) {
		this.adresseDao = adresseDao;
	}

	@Override
	protected String[] getConfigLocations() {
		return new String[] { "applicationContext.xml" };
	}
}
```


```
package at.gv.brz.dao.impl;

import org.hibernate.SessionFactory;

import at.gv.brz.dao.IAdresseDao;
import at.gv.brz.domain.Adresse;

public class AdresseDao implements IAdresseDao {

	private SessionFactory sessionFactory;

	public Adresse getAdresse(Integer adresseId) {
		return (Adresse) this.sessionFactory.getCurrentSession().get(
				Adresse.class, adresseId);
	}

	public Integer speicherAdresse(Adresse adresse) {
		Integer ret = (Integer) this.sessionFactory.getCurrentSession().save(
				adresse);
		return ret;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
```


```
package at.gv.brz.dao.impl;

import org.hibernate.SessionFactory;

import at.gv.brz.dao.IPersonDao;

public class PersonDao implements IPersonDao {

	private SessionFactory sessionFactory;

	public PersonDao getPerson(Integer personId) {
		return (PersonDao) this.sessionFactory.getCurrentSession().get(
				PersonDao.class, personId);
	}

	public Integer speicherPerson(PersonDao person) {
		Integer ret = (Integer) this.sessionFactory.getCurrentSession().save(
				person);
		return ret;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
```

Und hier noch Konfigurationsdateien:

```
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
	default-autowire="byType">

	<import resource="databaseContext.xml" />
	<import resource="transactionContext.xml" />

	<bean id="adresseDao" class="at.gv.brz.dao.impl.AdresseDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- rem: can't use dependency-check="objects" -->
	<bean id="personDao" class="at.gv.brz.dao.impl.PersonDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
</beans>
```


```
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="
[url=http://www.springframework.org/schema/beans]Index of /schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
[url=http://www.springframework.org/schema/jee]Index of /schema/jee[/url] http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"
	default-autowire="byType">

	<!-- hibernate template -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
		<!-- property name="cacheQueries" value="true" /-->
	</bean>
    
    <!-- Database -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="annotatedClasses">
			<list>
				<value>at.gv.brz.domain.Adresse</value>
				<value>at.gv.brz.domain.Person</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.jdbc.batch_size">0</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.cache.provider_class"> net.sf.ehcache.hibernate.SingletonEhCacheProvider
				</prop>
			</props>
		</property>
	</bean>
	<!-- mit externen Properties ... -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${db.driverClass}" />
		<property name="jdbcUrl" value="${db.jdbcUrl}" />
		<property name="user" value="${db.user}" />
		<property name="password" value="${db.password}" />
	</bean>

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:jdbc.properties" />
	</bean>
</beans>
```


```
<?xml version="1.0" encoding="UTF-8"?>
<beans default-init-method="init" default-destroy-method="destroy"
	xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
	[url=http://www.springframework.org/schema/beans]Index of /schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
	[url=http://www.springframework.org/schema/tx]Index of /schema/tx[/url] [url]http://www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]
	[url=http://www.springframework.org/schema/aop]Index of /schema/aop[/url] http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- deklarative Transaktion -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" propagation="REQUIRED" />
			<tx:method name="select*" read-only="true" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut id="daoMethods"
			expression="execution(* at.gv.brz.dao.impl.*Dao*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
	</aop:config>

	<aop:aspectj-autoproxy />

	<bean name="transactionTemplate"
		class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager" ref="transactionManager" />
	</bean>
</beans>
```


----------



## byte (5. Nov 2009)

Der Member adresseDao muss noch verdrahtet werden.

Du könntest es z.B. mit Autowiring machen:


```
@Autowired
private AdresseDao adresseDao;
```

[XML]<context:annotation-config />[/XML]


----------



## maki (5. Nov 2009)

Oder auch [c]@Resource[/c]


----------



## y0dA (5. Nov 2009)

Danke euch beiden für die Info, habe nun einmal versucht was byte gepostet hat nur gibts hierbei noch immer Probleme 

Also ich habe in meiner Testklasse den Member mit @Autowired "verdrahtet" und nun versucht in applicationContext.xml

```
<context:annotation-config />
[/JAVA 
zu adden - leider bekomme ich folgenden Fehler:

[CODE]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'at.gv.brz.dao.impl.AdresseDaoTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private at.gv.brz.dao.impl.AdresseDao at.gv.brz.dao.impl.AdresseDaoTest.adresseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [at.gv.brz.dao.impl.AdresseDao] is defined: Unsatisfied dependency of type [class at.gv.brz.dao.impl.AdresseDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:205)
	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:180)
	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:100)
	at junit.framework.TestCase.runBare(TestCase.java:132)
	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private at.gv.brz.dao.impl.AdresseDao at.gv.brz.dao.impl.AdresseDaoTest.adresseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [at.gv.brz.dao.impl.AdresseDao] is defined: Unsatisfied dependency of type [class at.gv.brz.dao.impl.AdresseDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
	at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
	... 20 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [at.gv.brz.dao.impl.AdresseDao] is defined: Unsatisfied dependency of type [class at.gv.brz.dao.impl.AdresseDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
	... 22 more
```

applicationContext.xml:

```
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
	[url=http://www.springframework.org/schema/context]Index of /schema/context[/url]
 http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byType">

	<context:annotation-config />

	<import resource="databaseContext.xml" />
	<import resource="transactionContext.xml" />

	<bean id="adresseDao" class="at.gv.brz.dao.impl.AdresseDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- rem: can't use dependency-check="objects" -->
	<bean id="personDao" class="at.gv.brz.dao.impl.PersonDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
</beans>
```

Entschuldigt wenn ich mich hierbei ein wenig dämlich anstelle, ist Neuland für mich zu konfigurieren- bisher musste ich nur beans definieren.

Werde nun auch mal @Resource ansehen, sagt mir nämlich auch nichts.


----------



## byte (5. Nov 2009)

Wird der ApplicationContext denn überhaupt angezogen? Das kannst Du einfach testen, indem Du in einer Bean (z.B. AdresseDao) einen Breakpoint im Konstruktor setzt und guckst, ob er da reinspringt.

Da fällt mir ein, Du musst der Testklasse auch noch mitteilen, welcher Context benutzt werden soll:


```
@ContextConfiguration(locations={"/testContext.xml"})
public class FoobarTest extends ... { ... }
```


----------



## y0dA (5. Nov 2009)

Macht

```
@Override
	protected String[] getConfigLocations() {
		return new String[] { "/applicationContext.xml" };
	}
```
nicht das gleiche wie

```
@ContextConfiguration(locations={"/testContext.xml"})
public class FoobarTest extends ... { ... }
```
[
?

Weiters ist mir gerade aufgefallen dass man gar nicht in den Konstruktor von AdresseDAO reinkomme, es wird auf der Konsole noch eine andere Exception angezeigt:

```
2009-11-05 13:30:24,734 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null
2009-11-05 13:30:24,734 ERROR [org.hibernate.util.JDBCExceptionReporter] - Connections could not be acquired from the underlying database!
2009-11-05 13:30:24,734 WARN [org.hibernate.cfg.SettingsFactory] - Could not obtain connection to query metadata
java.sql.SQLException: Connections could not be acquired from the underlying database!
	at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
	at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
	at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:111)
	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119)
	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339)
	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:822)
	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:748)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
	at org.springframework.test.AbstractSingleSpringContextTests.createApplicationContext(AbstractSingleSpringContextTests.java:213)
	at org.springframework.test.AbstractSingleSpringContextTests.loadContextLocations(AbstractSingleSpringContextTests.java:189)
	at org.springframework.test.AbstractSingleSpringContextTests.loadContext(AbstractSingleSpringContextTests.java:169)
	at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:140)
	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:98)
	at junit.framework.TestCase.runBare(TestCase.java:132)
	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
	at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
	at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
	at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
	... 56 more
2009-11-05 13:30:55,453 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null
2009-11-05 13:30:55,453 ERROR [org.hibernate.util.JDBCExceptionReporter] - Connections could not be acquired from the underlying database!
2009-11-05 13:30:55,453 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] - schema export unsuccessful
java.sql.SQLException: Connections could not be acquired from the underlying database!
	at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
	at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
	at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
	at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
	at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:252)
	at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:211)
	at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:353)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:822)
	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:748)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
	at org.springframework.test.AbstractSingleSpringContextTests.createApplicationContext(AbstractSingleSpringContextTests.java:213)
	at org.springframework.test.AbstractSingleSpringContextTests.loadContextLocations(AbstractSingleSpringContextTests.java:189)
	at org.springframework.test.AbstractSingleSpringContextTests.loadContext(AbstractSingleSpringContextTests.java:169)
	at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:140)
	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:98)
	at junit.framework.TestCase.runBare(TestCase.java:132)
	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
	at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
	at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
	at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
	... 57 more
```


----------



## byte (5. Nov 2009)

Offenbar fliegt er schon beim Erzeugen der DataSource auf die Nase. Bist Du sicher, dass die DB Daten alle richtig konfiguriert sind?


----------



## y0dA (5. Nov 2009)

Gut, hab nun eine db.jdbcUrl angegeben welche funktioniert (getestet mit einem anderen Projekt!). Geht trotzdem nicht, was dazu kommt ist dass ich ja normalerweise sehen müsste (in der Console) dass folgendes durchgeführt wird:

```
Loading context for locations: applicationContext.xml
```
Wird aber nicht angezeigt, also irgendwie wird wohl meine applicationContext.xml nicht gefunden oder geladen?


----------



## byte (5. Nov 2009)

Wenn das Logging richtig konfiguriert ist, solltest Du das sehen. Was funktioniert denn jetzt nicht? Gibts ne Exception? Hast Du mal Breakpoints gesetzt, ob die Testmethode ausgeführt wird?


----------



## y0dA (5. Nov 2009)

Danke für deine Geduld.

In der Testklasse wird nur

```
protected String[] getConfigLocations() {
		return new String[] { "applicationContext.xml" };
	}
```
aufgerufen, danach kommt folgende Exception:

```
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'at.gv.brz.dao.impl.AdresseDaoTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private at.gv.brz.dao.impl.AdresseDao at.gv.brz.dao.impl.AdresseDaoTest.adresseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [at.gv.brz.dao.impl.AdresseDao] is defined: Unsatisfied dependency of type [class at.gv.brz.dao.impl.AdresseDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:205)
	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:180)
	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:100)
	at junit.framework.TestCase.runBare(TestCase.java:132)
	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private at.gv.brz.dao.impl.AdresseDao at.gv.brz.dao.impl.AdresseDaoTest.adresseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [at.gv.brz.dao.impl.AdresseDao] is defined: Unsatisfied dependency of type [class at.gv.brz.dao.impl.AdresseDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
	at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
	... 20 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [at.gv.brz.dao.impl.AdresseDao] is defined: Unsatisfied dependency of type [class at.gv.brz.dao.impl.AdresseDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
	... 22 more
```


```
package at.gv.brz.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

import at.gv.brz.domain.Adresse;

public class AdresseDaoTest extends
		AbstractTransactionalDataSourceSpringContextTests {

	@Autowired
	private AdresseDao adresseDao;

	@Override
	protected void onSetUpInTransaction() throws Exception {
	}

	public void testSpeicherAdresse() {
		Adresse adresse = new Adresse("Stockerau", 2000, "Hauptstrasse 1337");
		Integer id = null;
		id = this.adresseDao.speicherAdresse(adresse);
		assertNotNull(id);
	}

	public void setAdresseDao(AdresseDao adresseDao) {
		this.adresseDao = adresseDao;
	}

	@Override
	protected String[] getConfigLocations() {
		return new String[] { "applicationContext.xml" };
	}
}
```


```
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
	[url=http://www.springframework.org/schema/context]Index of /schema/context[/url]
 http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byType">

	<context:annotation-config />

	<import resource="databaseContext.xml" />
	<import resource="transactionContext.xml" />

	<bean id="adresseDao" class="at.gv.brz.dao.impl.AdresseDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- rem: can't use dependency-check="objects" -->
	<bean id="personDao" class="at.gv.brz.dao.impl.PersonDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
</beans>
```

Hab ein Project refresh gemacht und nun bekomme ich auch Konsolenausgaben - siehe Anhang.


**EDIT**
Also im Log steht nichts --> java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE ist zu vernachlässigen.


----------



## byte (5. Nov 2009)

Laut Logs scheint der AppContext ja korrekt hochzufahren. Dann kommt ein close(). Dabei fliegt noch eine Exception bei der Datasource. Sicher, dass der Test nicht durchgelaufen ist? Setz mal einen Breakpoint in die Testmethode.


----------



## y0dA (5. Nov 2009)

Ich habe schon einen Breakpoint in die Testmethode gesetzt, er geht dort nicht rein.
Bekomme nach wie vor folgenden Fehler:

```
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'at.gv.brz.dao.impl.AdresseDaoTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private at.gv.brz.dao.impl.AdresseDao at.gv.brz.dao.impl.AdresseDaoTest.adresseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [at.gv.brz.dao.impl.AdresseDao] is defined: Unsatisfied dependency of type [class at.gv.brz.dao.impl.AdresseDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:205)
	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:180)
	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:100)
	at junit.framework.TestCase.runBare(TestCase.java:132)
	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private at.gv.brz.dao.impl.AdresseDao at.gv.brz.dao.impl.AdresseDaoTest.adresseDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [at.gv.brz.dao.impl.AdresseDao] is defined: Unsatisfied dependency of type [class at.gv.brz.dao.impl.AdresseDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
	at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
	... 20 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [at.gv.brz.dao.impl.AdresseDao] is defined: Unsatisfied dependency of type [class at.gv.brz.dao.impl.AdresseDao]: expected at least 1 matching bean
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:613)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
	... 22 more
```

Nun habe ich mich ein wenig bezüglich *@Autowired* eingelesen und gemerkt dass dies einfach die Anntotation für ein Property ist. Dies wollte ich ja eigentlich nicht (wir möchten mit XML die Dependencies eintragen).
Zur Testklasse, selbige erbt ja von *AbstractTransactionalDataSourceSpringContextTests* und selbige übernimmt doch die Dependency Injection sowie das Transaktionsmanagement für die Testklasse?


----------



## y0dA (5. Nov 2009)

Gut habe nun die fehlerhafte Stelle entdeckt, nämlich war der Member der Testklasse:
falsch:

```
private AdresseDao adresseDao;
```

richtig:

```
private IAdresseDao adresseDao;
```

Danke für die Unterstützung.


----------

