我对弹簧测试非常陌生。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContextTest.xml")
public class joinTest {
@Mock
@Autowired
private AccountDAO DAO;
@Test
public void testMethod()
{
(DAO).getJoin();
}
}
测试抱怨我在applicationContextTest中的transactionManager。显然我是用容器运行的。我真的不知道如何在Spring Test上下文中处理事务。
<context:component-scan base-package="com.abstinence.Logic"/>
<context:annotation-config/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://xx.xx.xx.xx/testdb"/>
<property name="username" value="SA"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.abstinence.Logic"/>
<property name="hibernateProperties">
<props>
<prop key ="dialect">org.hibernate.dialect.HSQLDialect</prop>
</props>
</property>
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="hibernateDAOOperation" expression="execution(* com.abstinence.Logic.AccountDAO.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="hibernateDAOOperation"/>
</aop:config>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
我得到的确切错误是:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDAO' defined in file [/home/user/NetBeansProjects/WebAbstinenceMaven/target/classes/com/abstinence/Logic/AccountDAO.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txAdvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContextTest.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getConnectionProvider()Lorg/hibernate/service/jdbc/connections/spi/ConnectionProvider;
我已经困惑了几天试图解决这个问题。有人能指出正确的方向吗?
答案 0 :(得分:0)
这可能是由于Spring与Hibernate 4.3.0.Beta1的兼容性问题以及更新报告的here。 如果这是问题,降级到Hibernate 4.1.7.Final应该可以解决问题。