Junit结合Spring对Dao层进行单元测试

实际开发过程中,写单元测试是非常难的一件事情,其主要原因是代码结构不够好,导致单元测试不好写。特别是Dao层,因为Dao层代码都是与数据库相关的,所以我们在对Dao层代码进行单元测试的时候,不仅仅要考虑我在上篇文章中提到的代码隔离,还要注意单元测试不能带来脏数据。另外,dao层实例依赖spring上下文,我们怎么样来解决这个问题?

  看看下面的一个的测试实例:

 


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/META-INF/spring/sellmanager-context.xml",
"/META-INF/spring/dao-context.xml",
"/META-INF/spring/mvc-context.xml" })
//@Transactional
public class SysEmployeeDaoTest {

@Test
public void deleteEmployee() {
Employee employee = new Employee();
employee.setEmployeeCode(""+new Date().getTime());
employee.setEmployeeName("lisanlai");
employee.setDelFlag("0");
String empId = sysEmployeeDao.save(employee);
Assert.assertNotNull("新增的员工ID为null",empId);
//把该id对应的员工删除
sysEmployeeDao.deleteEmployee(empId);
//再用该ID去查数据库,如果为空,说明删除方法逻辑正确
Employee emp = sysEmployeeDao.get(empId);
Assert.assertNotNull(emp);
Assert.assertArrayEquals("deleteEmployee方法逻辑不正确,员工没有被删除",
new String[]{"1"}, new String[]{emp.getDelFlag()});
//删除员工对象
sysEmployeeDao.delete(emp);
}

@Test
@Transactional
@Rollback(true)
public void saveEmployee() {
Employee employee = new Employee();
employee.setEmployeeName("lisanlai");
String empCode = ""+new Date().getTime();
employee.setEmployeeCode(empCode);
sysEmployeeDao.saveEmployee(employee);
//通过code查找员工
List emps = sysEmployeeDao.findByNamedParam(
new String[]{"employeeCode"},
new String[]{empCode});
Assert.assertTrue("saveEmployee方法逻辑错误,员工保存失败!", !emps.isEmpty());
}
}

 注意类上的三个注解:

 

//指定测试用例的运行器 这里是指定了Junit4
@RunWith(SpringJUnit4ClassRunner.class)
//指定Spring的配置文件 路径相对classpath而言
@ContextConfiguration({ "/META-INF/spring/sellmanager-context.xml",
"/META-INF/spring/dao-context.xml",
"/META-INF/spring/mvc-context.xml" })
//如果在类上面使用该注解,这样所有的测试方案都会自动的 rollback
//@Transactional
再注意saveEmployee方法上的两个注解:
//这个注解表示使用事务
@Transactional
//这个表示方法执行完以后回滚事务,如果设置为false,则不回滚
@Rollback(true)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring进行单元测试,通常需要配置测试环境。以下是配置Spring单元测试的步骤: 1. 添加JUnitSpring Test依赖到项目中。 2. 创建配置文件 applicationContext.xml。 3. 在配置文件中定义需要测试的组件。 4. 创建测试类并在测试类中注入需要测试的组件。 5. 使用JUnit框架编写测试方法。 以下是一个示例配置文件 applicationContext.xml: ``` <?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.example.project" /> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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="password" /> </bean> <!-- 配置持久化 --> <bean id="userDao" class="com.example.project.dao.UserDaoImpl"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置业务 --> <bean id="userService" class="com.example.project.service.UserServiceImpl"> <property name="userDao" ref="userDao" /> </bean> </beans> ``` 在测试类中,可以使用注解 @RunWith(SpringJUnit4ClassRunner.class) 来指定使用SpringJUnit4ClassRunner来运行测试,使用注解 @ContextConfiguration(locations = "classpath:applicationContext.xml") 来指定加载配置文件。 以下是一个示例测试类: ``` @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class UserServiceTest { @Autowired private UserService userService; @Test public void testGetUser() { User user = userService.getUserById(1L); assertNotNull(user); assertEquals("test", user.getName()); } } ``` 这个测试类中,我们注入了业务的 UserService 组件,并编写了一个测试方法 testGetUser,用来测试获取用户信息的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值