java - Spring集成测试-配置中的事务声明似乎破坏了测试

标签 java spring hibernate integration-testing spring-test

我正在使用 Spring MVC + Hibernate + MySQL“Hello World”应用程序,目前正在尝试使用 jUnit 在 Spring MVC Controller 上运行以下集成测试。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:src/main/webapp/WEB-INF/springapp-servlet.xml"})
public class InventoryControllerIT
{
    @Autowired
    private InventoryController controller;

    @Test
    public void handleRequest_anyRequest_returnsSuccessfully() throws Exception
    {       
        ModelAndView modelAndView = this.controller.handleRequest(null, null);
    }
}

但是,每次我这样做时都会遇到以下异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [springapp.web.InventoryController] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

以前我没有实现任何实际的数据访问并且测试顺利通过,但现在我添加了 DAO 的 Hibernate 实现以及 Spring 事务管理,我收到此错误。以下是我的小程序上下文配置 xml 的相关部分:

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">


    <bean name="/hello.htm" class="springapp.web.InventoryController">
        <property name="productManager" ref="productManager" />
        <property name="productDao" ref="productDao" />
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


    ...


    <!-- Hibernate -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>

        <property name="mappingJarLocations">
            <list>
                <value>WEB-INF/lib/springapp-dataaccess*.jar</value>
            </list>
        </property>
    </bean>

    <bean id="productDao" class="springapp.dataaccess.dao.ProductHibernateDao">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" />

</beans>

如果我删除 <tx:annotation-driven />从配置来看,上述异常不会发生,但测试会失败,因为处理程序中发生的数据访问调用不再有打开的事务。该应用程序在测试之外运行得很好。有人对问题是什么有任何想法吗?

最佳答案

InventoryController实现任何接口(interface)时,Spring默认使用基于接口(interface)的代理对其应用事务方面。这样的代理实现了 InventoryController 的接口(interface),但它不是 InventoryController 的子类,因此它不能注入(inject)到 InventoryController 类型的字段中。

您需要使用接口(interface)作为要 Autowiring 的字段类型,或者配置 Spring 以应用基于目标类的代理。

另请参阅:

关于java - Spring集成测试-配置中的事务声明似乎破坏了测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10078519/

相关文章:

java - 如何使用 Hibernate 进行级联删除

eclipse - 如何禁用 Hibernate 泛洪日志

java - 为什么java编译器不重写这段代码?

java - 消息监听器中的 JMS 连接池

java - 无法使用 oauth2 添加 spring security 的自定义配置

java - 使用自定义上下文 XML 时如何使用 URL API 部署 Tomcat 应用程序

java - 注入(inject)静态属性值

java - 如何在android中覆盖ActionMode的onActionItemClicked

java - 时间无关的 JUnit 测试用例

mysql - 如何在hibernate中将一个实体类映射到具有相同sql模式的四个表?