gpt4 book ai didi

java - 使用 JUnit4 进行测试时,Spring @transactional 不会启动事务

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:00:53 27 4
gpt4 key购买 nike

我有以下配置。

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

<bean id="dataSource" class="org.springframework.jdbc.datasource.IsolationLevelDataSourceAdapter">
<property name="targetDataSource">
<bean class="com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource">
<property name="user" value="user"/>
<property name="password" value="password"/>
<property name="serverName" value="someserver"/>
<property name="databaseName" value="someDBName"/>
<property name="portNumber" value="somePortNumber"/>
</bean>
</property>
</bean>

<!-- this is bean is only used for data extraction module only -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" lazy-init="true">
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
<property name="dataSource" ref="dataSource"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<!--
Instruct Spring to perform declarative transaction management automatically
on annotated classes. transaction-manager="transactionManager"
-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

然后,当我运行具有插入语句的测试时,它们会产生这样的错误消息:

javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:47)

经过深思熟虑,我尝试了这个:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:services.xml" })
@Transactional(propagation = Propagation.REQUIRED)
@TransactionConfiguration(defaultRollback = true)
@TestExecutionListeners(value = { DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})
public class SimpleUnitTest {

@Test
public void simpleTest() throws Exception {
System.out.println(entityManager.getTransaction().isActive());
assertTrue(entityManager.getTransaction().isActive());
}
}

它失败了。 entityManager.getTransaction().isActive() 实际上是错误的。

为什么交易测试不会开始交易?

最佳答案

你需要添加

@TestExecutionListeners(TransactionalTestExecutionListener.class)

或延伸自

AbstractTransactionalJUnit4SpringContextTests

获取交易行为。 (请记住,您的测试类不是 bean,因此常规事务配置不适用)

关于java - 使用 JUnit4 进行测试时,Spring @transactional 不会启动事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3350698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com