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

Java Spring集成测试-配置中的事务声明似乎破坏了测试,java,spring,hibernate,integration-testing,spring-test,Java,Spring,Hibernate,Integration Testing,Spring Test,我正在使用SpringMVC+Hibernate+MySQL“HelloWorld”应用程序,目前正在尝试使用jUnit在SpringMVC控制器上运行以下集成测试 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"file:src/main/webapp/WEB-INF/springapp-servlet.xml"}) public class InventoryControllerIT { @Autowir

我正在使用SpringMVC+Hibernate+MySQL“HelloWorld”应用程序,目前正在尝试使用jUnit在SpringMVC控制器上运行以下集成测试

@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.
以前我没有实现任何真正的数据访问,测试通过得很好,但是现在我在spring事务管理中添加了DAO的Hibernate实现,我得到了这个错误。以下是我的小程序上下文配置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>

...
org.hibernate.dialogue.mysqldialogue
真的
WEB-INF/lib/springapp dataaccess*.jar

如果我从配置中删除
,则不会发生上述异常,但测试失败,因为处理程序中发生的数据访问调用不再具有打开的事务。该应用程序在测试之外运行正常。有人知道问题是什么吗?

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

您要么需要将接口用作要自动连接的字段类型,要么将Spring配置为应用基于目标类的代理

另请参见:


在运行我正在构建的一个小型库的单元测试时,我遇到了类似的问题

替换您的:

<tx:annotation-driven />


注意,在我的项目中,我还必须为单元测试添加以下依赖项(maven项目):

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
        <scope>test</scope>
    </dependency>

cglib
cglib
2.2.2
测试

致以问候。

您是否尝试将InventoryControllerIT类添加到hibernate配置xml中?
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
        <scope>test</scope>
    </dependency>