Spring3整合Hibernate 项目,Dao测试(新)

最近的项目用的spring3.x,测试Dao时发现AbstractTransactionalDataSourceSpringContextTests已经不推荐使用了,看spring帮助文档、源码,重新写了一个测试类如下:

import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; /** * @author ZhengGuang Dong */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-config.xml") //加载基本的数据源、事务管理等公用配置 @TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true) @Transactional public class SpringDaoTestBase { }

这是个测试基础类

 

Dao测试

 

import com.gwtjs.model.Module; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; /** * @author ZhengGuang Dong */ public class EntityDaoTest extends SpringDaoTestBase { private EntityDao entityDao; @Test @Rollback(false) public void testSave() { Module m = new Module(); m.setText("Test 1"); m.setDescription("Emtity DAO Test"); entityDao.saveOrUpdate(m); } @Autowired public void setEntityDao(EntityDao entityDao) { this.entityDao = entityDao; } }

 

测试结果:

1,junit   绿了,5.5.xxx秒

 

2.数据库(oracle)值:

SQL> select id,text,description from t_module;
 
         ID TEXT                                                                             DESCRIPTION
----------- ---------------------------------------                                   ------------------- ------
         82 Test 2                                                                           Emtity DAO Test
         84 402881d82f6c6c49012f6c6c499e0000                         Emtity DAO Test
         85 402881d82f6c6cfc012f6c6cfc050000                            Emtity DAO Test
         81 Test 1                                                                           Emtity DAO Test

 

 

增加个单元集:

import org.junit.runner.RunWith; import org.junit.runners.Suite.SuiteClasses; /** * Created by IntelliJ IDEA. * User: ZhengGuang Dong * Date: 11-4-8 * Time: 上午9:27 * To change this template use File | Settings | File Templates. */ @RunWith(org.junit.runners.Suite.class) @SuiteClasses({EntityDaoTest.class}) public class DaoTestAll { }

 

 

你可能感兴趣的:(DAO,spring,Hibernate,Module,测试,Autowired)