简体   繁体   English

无法在JUnit测试类中自动装配Spring bean

[英]Cannot autowire Spring bean in JUnit test class

I would like to try embedded database for testing my DAO objects in Spring application. 我想尝试嵌入式数据库在Spring应用程序中测试我的DAO对象。

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: 我的JUnit测试类需要与此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: 一切正常(创建了“ dataSourceEmbedded” bean),但是当我尝试在PartnerDAOTest类中自动装配它们时,Spring抛出此异常:

testSavePartner(sandbox.PartnerDAOTest): Error creating bean with name 'sandbox.PartnerDAOTest': Injection of autowired dependencies failed; testSavePartner(sandbox.PartnerDAOTest):创建名称为'sandbox.PartnerDAOTest'的bean时出错:自动连接依赖项的注入失败; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.jdbc.datasource.embedded.EmbeddedDatabase sandbox.PartnerDAOTest.dataSourceEmbedded; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段: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. 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有发现依赖项类型为[org.springframework.jdbc.datasource.embedded.EmbeddedDatabase]的合格Bean:期望至少有1个Bean可以作为此依赖项的自动装配候选。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=dataSourceEmbedded)} 依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = dataSourceEmbedded)}

What is wrong with my code? 我的代码有什么问题?

As @M.Deinum noticed in his comment: 正如@ M.Deinum在他的评论中注意到的那样:

There is no bean of the type EmbeddedDatabase there is a DataSource. 没有类型为EmbeddedDatabase的bean,没有数据源。 The embedded-database eventually creates a DataSource not an EmbeddedDatabase. 嵌入式数据库最终会创建一个数据源,而不是EmbeddedDatabase。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM