Java 弹簧3@Autowire处于测试中
我现在有一个恼人的问题。 由于自动连线,我的测试失败 无法自动连线字段:private k.dao.CompanyDao k.dao.CompanyDaoTest.CompanyDao;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项类型为[k.dao.CompanyDao]的匹配bean:至少需要1个符合此依赖项autowire候选项条件的bean 我认为@ContextConfiguration可能是问题所在 测试Java 弹簧3@Autowire处于测试中,java,spring-3,spring-test,Java,Spring 3,Spring Test,我现在有一个恼人的问题。 由于自动连线,我的测试失败 无法自动连线字段:private k.dao.CompanyDao k.dao.CompanyDaoTest.CompanyDao;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项类型为[k.dao.CompanyDao]的匹配bean:至少需要1个符合此依赖项autowire候选项条件的bean 我认为@ContextConfigurati
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:**/servlet-context.xml", "classpath:**/root-context.xml", "classpath:**/ccc-jpa.xml" })
public final class CompanyDaoTest {
@Autowired
private CompanyDao companyDao;
@Test
public void testTest() {
}
}
公司道
public interface CompanyDao extends GenericDao<Company> {
}
公共接口公司DAO扩展了GenericDao{
}
道吉帕公司
@Repository("companyDao")
public class CompanyDaoJpa extends GenericDaoJpa<Company> implements CompanyDao {
public CompanyDaoJpa() {
super(Company.class);
}
}
@Repository(“companyDao”)
公共类CompanyDaoJpa扩展了GenericDaoJpa实现了CompanyDao{
上市公司daojpa(){
超级(公司级);
}
}
通用刀
public interface GenericDao<T extends DomainObject> {
public T get(Long id);
public List<T> getAll();
public T save(T object);
public T delete(T object);
}
公共接口GenericDao{
公共T get(长id);
公共列表getAll();
公共T保存(T对象);
公共T删除(T对象);
}
servlet-context.xml
<annotation-driven />
<context:component-scan base-package="k"/>
我猜您的测试根本没有加载
servlet context.xml
您将servlet context.xml
作为类路径资源引用,但servlet context.xml
通常位于WEB-INF
下,它不是应用程序类路径的一部分。请注意,当使用通配符(classpath:*/servlet context.xml
)引用配置文件时,Spring不会抱怨缺少配置文件,因此即使找不到配置文件,测试也会以静默方式启动
从单元测试中访问WEB-INF
中的SpringXML文件没有好方法。如果要对它们运行测试,则需要将它们移动到类路径(即,移动到类似于src
或resources
,具体取决于项目布局)。由于DispatcherServlet
和ContextLoaderListener
希望在WEB-INF
下找到这些文件,因此您还需要使用各自的contextConfigLocation
参数重新配置它们。例如,在DispatcherServlet
的情况下:
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:**/servlet-config.xml</param-value>
</init-param>
上下文配置位置
类路径:*/servlet-config.xml
我猜您的测试根本没有加载servlet context.xml
您将servlet context.xml
作为类路径资源引用,但servlet context.xml
通常位于WEB-INF
下,它不是应用程序类路径的一部分。请注意,当使用通配符(classpath:*/servlet context.xml
)引用配置文件时,Spring不会抱怨缺少配置文件,因此即使找不到配置文件,测试也会以静默方式启动
从单元测试中访问WEB-INF
中的SpringXML文件没有好方法。如果要对它们运行测试,则需要将它们移动到类路径(即,移动到类似于src
或resources
,具体取决于项目布局)。由于DispatcherServlet
和ContextLoaderListener
希望在WEB-INF
下找到这些文件,因此您还需要使用各自的contextConfigLocation
参数重新配置它们。例如,在DispatcherServlet
的情况下:
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:**/servlet-config.xml</param-value>
</init-param>
上下文配置位置
类路径:*/servlet-config.xml
其他Spring XML配置是什么样子的?其他Spring XML配置是什么样子的?谢谢,这就是问题所在:)谢谢,这就是问题所在:)