2
votes

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?

1
The stacktrace is explicit: Error creating bean with name sandbox.PartnerDAOTest: Injection of autowired dependencies failed. Could not autowire field: public org.springframework.jdbc.datasource.embedded.EmbeddedDatabase.Luiggi Mendoza
Ok, thanks, I can read the exception it but I am sure everything is ok (or I have no idea what is wrong), because bean is created...user2148736
Well, it is not, otherwise the injection would be right. What I'm saying is, the problem cannot be checked with the current information you've posted.Luiggi Mendoza
What another information do you need to check the problem?user2148736
There is no bean of the type EmbeddedDatabase there is a DataSource. The embedded-database eventually creates a DataSource not an EmbeddedDatabase.M. Deinum

1 Answers

0
votes

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.