首页 文章

用@Autowired发布模拟spring jdbctemplate

提问于
浏览
1

我使用mockito作为模拟对象库 . 我是单元测试DAO .

DAO期望通过@Autowired注入JdbcTemplate . 因此,在单元测试可以调用的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

以前有没有人用这种方法取得成功?

谢谢 .

2 回答

  • 0

    我认为问题是返回泛型类型的工厂方法 mock . 擦除后, spring 无法扣除对象类型,尝试通过 class 属性提供显式对象类型 .

    public MockitoMockFactoryBean implements FactoryBean<?>
    {
        private Class<?> objectType;
    
        @Override public Object getObject()
        {
            return Mockito.mock(objectType);
        }
        public void setObjectType(Class<?> objectType)
        {
            this.objectType=objectType;
        }
        @Override public Class<?> getObjectType()
        {
            return objectType;
        }
    }
    
  • 0

    还有一些其他技术可以帮助您解决测试问题:

    使用您的真实数据库:

    Testing的Spring Framework支持允许您在安装过程中启动事务,并在拆解时将它们回滚,因此数据库不会充满测试数据 . 对于复杂的设置DBUnit可以提供帮助 . 正如您所指出的,这比单元测试需要更长的时间 . 这就是为什么开发人员经常将集成测试分离到一个单独的套件中,该套件作为构建步骤而不是开发人员构建的一部分运行 .

    使用MockRunner:

    MockRunner是一个用于创建伪JDBC组件的库,您可以在其中填充结果集数据,而JdbcTemplate不知道其中的差异 . 我不确定你是否可以使用Spring创建一个MockDataSource,而无需编写一些辅助类

    在没有Spring接线的测试中使用Mockito:

    Mockito最适合用于单元测试 . 因此,您的DAO测试不会使用Spring连接,您可以创建一个单元测试,在其中创建您手动注入DAO的模拟JdbcTemplate .

    如果你试图单独测试DAO以外的东西,并想看看几个组件如何组合在一起进行真正的集成测试,那么模拟数据库将隐藏真实数据库弹出的真正问题 . 如果您只是关心您的个人DAO使用JdbcTemplates的模拟输出正常运行,那么使用Mockito或MockRunner将允许您这样做 .

相关问题