Reputation: 1323
I would like to try embedded database for testing my DAO objects in Spring application.
In application context I have this tag:
<jdbc:embedded-database id="dataSourceEmbedded" type="HSQL">
<jdbc:script location="classpath:/embeddeddb/schema.sql" />
<jdbc:script location="classpath:/embeddeddb/data.sql" />
</jdbc:embedded-database>
my JUnit test class needs to work with this bean:
import org.apache.log4j.Logger;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/applicationContext.xml")
public class PartnerDAOTest {
@Autowired
@Qualifier("dataSourceEmbedded")
public EmbeddedDatabase dataSourceEmbedded;
@Test
public void testSavePartner() {
}
}
everything works ("dataSourceEmbedded" bean is created) but when I try to autowire them in PartnerDAOTest class Spring throws this exception:
testSavePartner(sandbox.PartnerDAOTest): Error creating bean with name 'sandbox.PartnerDAOTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.jdbc.datasource.embedded.EmbeddedDatabase sandbox.PartnerDAOTest.dataSourceEmbedded; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.datasource.embedded.EmbeddedDatabase] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=dataSourceEmbedded)}
What is wrong with my code?
Upvotes: 2
Views: 2198
Reputation: 1323
As @M.Deinum noticed in his comment:
There is no bean of the type EmbeddedDatabase there is a DataSource. The embedded-database eventually creates a DataSource not an EmbeddedDatabase.
Upvotes: 0