测试的数据库生成文件
<?xml version="1.0" encoding="UTF-8"?> <dataset> <branch id="1" name="测试1" sub_id="1"/> <branch id="2" name="测试2" sub_id="1"/> <branch id="3" name="测试3" sub_id="1"/> <branch id="4" name="测试4" sub_id="1"/> <branch id="5" name="测试5" sub_id="1"/> <branch id="6" name="测试6" sub_id="1"/> </dataset> |
单元测试类:BranchDaoTest
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/beans.xml") public class BranchDaoTest extends AbstractDbUnitTestCase { @Inject private SessionFactory sessionFactory; @Inject private IBranchDao branchDao; @Inject private ISubjectDao subjectDao; @Before public void setUp() throws SQLException, IOException, DatabaseUnitException { //此时最好不要使用Spring的Transactional来管理,因为dbunit是通过jdbc来处理connection,再使用spring在一些编辑操作中会造成事务shisu Session s = sessionFactory.openSession(); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s)); this.backupAllTable(); IDataSet ds = createDateSet("t_beans"); DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds); } @After public void tearDown() throws DatabaseUnitException, SQLException, IOException { SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); Session s = holder.getSession(); s.flush(); TransactionSynchronizationManager.unbindResource(sessionFactory); this.resumeTable(); } @Test public void testLoadByName(){ Branch branch=branchDao.loadByName("测试一"); assertEquals(branch.getId(), 1); } } |
测试结果