简体   繁体   中英

JUnit Test not rollbacking with SpringJUnit4ClassRunner

To put things in perspective, everything was working fine untill I had to format my working station. After I've managed to get the project running I ran the JUnit tests and most of my tests broke. The problem was because the database is not rolling back. Maybe I missed on some configuration, maybe on mysql it self.

So, these are my configuration/source files:

A JUnit Class declaration:

@ContextConfiguration(locations = { "classpath*:testApplicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class ClientServiceTest {

The applicationContext for test scope:

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${driver}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${user}"/>
    <property name="password" value="${password}"/>
    <property name="defaultAutoCommit" value="false" />

    </bean>

Check what engine you use for application tables. MyISAM does not support transactions. You should use InnoDB.

To check engine, use query:

SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = 'your_database_name'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM