1

Simple DAO Layer wiht applicationContext.xml please find below my applicationContect.xml please help me to understand why my Junit says successful but data are not inserting into the db.

<?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:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
   xsi:schemaLocation="  
http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd  
http://www.springframework.org/schema/tx  
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.VideoRental.*" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>com.VideoRental.domain.Category</value>
<value>com.VideoRental.domain.Customer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.hbm2ddl.import_files">import.sql</prop>  
<prop key="connection.autocommit">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop> 

</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="mySessionFactory"/>
</bean>

</beans>
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Thiru
  • 759
  • 2
  • 9
  • 19

2 Answers2

2

By default, @Transactional tests will roll back all changes when the test method completes. So, even if the test passes, the changes are only valid for the currently running transaction. To persist your changes, you need to commit the transaction.

Check out the example on this answer for more details.

You can replace @Transactional with transactionTemplate to commit the transaction prior to finishing a test.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
0

If you are using @Transactional annotation driven in your project, you can simply set

@TransactionConfiguration(defaultRollback = false, transactionManager = "YourTransactionManager")

in your test case will work perfectly. I've researched for days and worked good finally in my local test cases, hopefully this will helps you too.

clemens
  • 16,716
  • 11
  • 50
  • 65
Wilson
  • 11
  • 3