【发布时间】:2011-10-28 13:12:42
【问题描述】:
我使用 mockito 作为模拟对象库。我正在对 DAO 进行单元测试。
DAO 期望 JdbcTemplate 通过@Autowired 注入。因此,DAO 中没有单元测试可以调用的 JDBC 模板的 setter 方法。
我有以下测试 spring 应用程序上下文:
<b:beans
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:annotation-config />
<b:bean id="mockito" class="org.mockito.Mockito" />
<b:bean
id="mockJdbcTemplate"
factory-bean="mockito"
factory-method="mock">
<b:constructor-arg value="org.springframework.jdbc.core.JdbcTemplate"/>
</b:bean>
</b:beans>
我期待在测试执行时,spring 将创建模拟 jdbctemplate 实例并将其自动连接到 DAO。
但这并没有发生 - 相反,我只是得到以下异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.jdbc.core.JdbcTemplate] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
... 42 more
以前有人用这种方法成功过吗?
谢谢。
【问题讨论】:
-
我看不到模拟 JdbcTemplate 的意义。通过模拟 JdbcTemplate 来测试 DAO 有什么意义?你的测试将测试什么?
-
好吧,我们有一个单独的 DAO 层项目,这是唯一一个代码覆盖率为 0% 的项目,因为我们跳过了为 DAO 编写单元测试,因为大多数方法体都是一个内衬(this.jdbcTemplate .query 或 update) - 但是在外部审计期间,这个(没有测试覆盖)被选中,所以我正在编写单元测试来勾选复选框......
-
我认为这个DAO项目应该使用集成测试,而不是单元测试。因此,您应该创建一个测试数据库并检查 DAO 方法是否产生正确的结果。另外,看看 DbUnit。
-
由于各种原因(缺少 Sybase ASE 查询语法支持),我无法使用嵌入式数据库。使用嵌入式数据库进行测试也不是真正的测试...使用独立的测试数据库会减慢测试速度执行……我们有 7 名开发人员和一台 CI 服务器……所以测试不会孤立地执行……
-
所以实际上测试根本不会测试任何东西,只是用来人为地增加覆盖率。对不起,但这真的很愚蠢。您不妨使用真正的 JdbcTemplate 并捕获并忽略测试中的任何异常。