Unitils集成DbUnit,数据库配置。
unitils.properties
unitils.modules=database,dbunit,hibernate,spring, #unitils.module.dbunit.className=sample.unitils.module.CustomExtModule #database database.driverClassName=com.mysql.jdbc.Driver database.url=jdbc:mysql://localhost:3306/ams database.dialect = mysql database.userName=root database.password=123 database.schemaNames=ams # The database maintainer is disabled by default. updateDataBaseSchema.enabled=true #This table is by default not created automatically dbMaintainer.autoCreateExecutedScriptsTable=true dbMaintainer.script.locations=E:/workspace_ams_1/ams/resources/dbscripts dbMaintainer.script.fileExtensions=sql DbUnitModule.DataSet.factory.default=com.unitils.dataset.excel.MultiSchemaXlsDataSetFactory DbUnitModule.ExpectedDataSet.factory.default=com.unitils.dataset.excel.MultiSchemaXlsDataSetFactory #DbUnitModule.DataSet.loadStrategy.default=org.unitils.dbunit.datasetloadstrategy.impl.CleanInsertLoadStrategy DatabaseModule.Transactional.value.default=commit # XSD generator dataSetStructureGenerator.xsd.dirName=resources/xsd dbMaintainer.generateDataSetStructure.enabled=true sequenceUpdater.sequencevalue.lowestacceptable=100
测试用例基类UnitilsTest.java
package com.xxxx.ams.base; import org.springframework.context.ApplicationContext; import org.unitils.UnitilsJUnit4; import org.unitils.spring.annotation.SpringApplicationContext; @SpringApplicationContext({"spring-context.xml"}) public class UnitilsTest extends UnitilsJUnit4 { @SpringApplicationContext protected ApplicationContext applicationContext; }
测试Service类
package com.xxxx.ams.service; import java.util.Date; import javax.annotation.Resource; import static org.junit.Assert.*; import org.apache.commons.codec.digest.DigestUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import com.xxxx.ams.base.BaseTestCase; import com.xxxx.ams.entity.User; import com.xxxx.ams.service.UserService; /** * 测试用户操作 * 1、创建用户 * 2、修改用户信息 * 3、根据邮箱查找用户 * 4、检查邮箱是否存在 * 5、删除用户 * */ public class UserServiceTest extends BaseTestCase{ @Resource private UserService userService; @Before public void init(){ } @Test public void testSaveUser(){ if(!userService.isExistByEmail("[email protected]")){ User user = new User(); user.setPassword("123456"); String passwordMd5 = DigestUtils.md5Hex(user.getPassword()); user.setPassword(passwordMd5); user.setCreateDate(new Date()); user.setEmail("[email protected]"); user.setIsAccountEnabled(true); user.setIsAccountExpired(false); user.setIsAccountLocked(false); user.setIsCredentialsExpired(false); user.setName("xxxx"); user.setUsername("xxxx"); userService.save(user); } } @Test public void testUpdateUser(){ User po = userService.getUserByEmail("[email protected]"); Assert.assertNotNull(po); po.setName("aaaa"); userService.update(po); User po2 = userService.getUserByEmail("[email protected]"); assertEquals("aaaa", po2.getName()); } @Test public void testGetUserByEmail() { User user = userService.getUserByEmail("[email protected]"); Assert.assertNotNull(user); } @Test public void testCheckUserEmailExsit() { boolean flag = userService.isExistByEmail("[email protected]"); Assert.assertTrue(flag); } @Test public void testDeleteUser() { User po = userService.getUserByEmail("[email protected]"); userService.delete(po); boolean flag = userService.isExistByEmail("[email protected]"); assertEquals(false, flag); } @After public void destroy(){ } }
测试Dao层
创建Excel文件,用于数据验证。(ams_user.xls , ams_user.expected.xls)
package com.xxxx.ams.dao; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import java.util.List; import org.junit.Test; import org.unitils.dbunit.annotation.DataSet; import org.unitils.dbunit.annotation.ExpectedDataSet; import org.unitils.spring.annotation.SpringBeanByType; import com.unitils.dataset.util.XlsDataSetBeanFactory; import com.xxxx.ams.base.UnitilsTest; import com.xxxx.ams.entity.User; public class UserDaoTest extends UnitilsTest { @SpringBeanByType private UserDao userDao; @Test @DataSet("ams_user.xls") // 准备数据 public void findUserByEmail() { User user = userDao.getUserByEmail("[email protected]"); assertNull("不存在邮箱为[email protected]的用户!", user); user = userDao.getUserByEmail("[email protected]"); assertNotNull("[email protected]邮箱存在!", user); assertEquals("xxxx-1", user.getUsername()); } // 验证数据库保存的正确性 @Test @ExpectedDataSet("ams_user.expected.xls") // 准备验证数据 public void saveUser() throws Exception { // 通过XlsDataSetBeanFactory数据集绑定工厂创建测试实体 User u = XlsDataSetBeanFactory.createBean("ams_user.xls", "ams_user", User.class); userDao.save(u); // 执行用户信息更新操作 } // 验证数据库保存的正确性 @Test @ExpectedDataSet("ams_user.expected.xls") // 准备验证数据 public void saveUsers() throws Exception { List<User> users = XlsDataSetBeanFactory.createBeans( "ams_user.xls", "ams_user", User.class); for (User u : users) { userDao.save(u); } } }