[英]@Transactional doesn't rollback on DAO @Test methods
我創建了一個MySQL數據庫並填充了測試用途行。 我想在這個數據庫上進行DAO單元測試。 每個@Test
都是@Transactional
因此每次測試后都會進行回滾。 不幸的是,由於我的數據庫仍在進行更改,因此無法正常工作。
我正在使用以下context.xml加載我的Spring配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/push_test" />
<property name="username" value="push_dao" />
<property name="password" value="pushpassword" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userPushDAO" class="my.package.userPushDAOImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
這個 stackoverflow問題說我
必須在應用程序上下文中提供
PlatformTransactionManager
bean
但即使使用它(它是我的上下文中的transactionManager
)也沒有任何反應,我的數據庫仍然被修改而不是回滾。
這是我的DAO測試類
public class UtilisateurPushDAOImplTest {
private static ApplicationContext ctx;
private static UserPushDAO userPushDAO;
@BeforeClass
public static void doSetup() {
ctx = new ClassPathXmlApplicationContext("context.xml");
userPushDAO = (userPushDAO) ctx.getBean("userPushDAO");
}
@Test
@Transactional
public void test() {
userPushDAO.deleteById("id");
}
}
我是否在配置中或在理解 @Transactional
應該如何工作/做什么的過程中遺漏了什么?
正如@ M.Deinum在評論部分發表的那樣,
測試中的
@Transactional
僅適用於SpringRunner
作為單元測試運行器。如果您自己創建應用程序上下文,它將不起作用。
另外,不要忘記將事務模式添加到context.xml
,以便能夠正確加載上下文文件。
DAO測試類
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/context.xml"})
public class UserPushDAOImplTest {
@Resource(name="userPushDAO")
private UserPushDAO userPushDAO;
@Test
@Transactional
public void test() {
userPushDAO.deleteById("id");
}
}
的context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/push_test" />
<property name="username" value="push_dao" />
<property name="password" value="pushpassword" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userPushDAO" class="my.package.userPushDAOImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.