Spring 使用介绍(十)—— 单元测试

一、概述

Spring测试框架提供了对单元测试的支持,以便使用spring的依赖注入和事务管理功能

maven依赖:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    <scope>test</scope>
</dependency>

二、简单示例

业务接口及实现类

public interface UserService {
    void addUser(String name, int age);
    void updateUserName(String name);
}
@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private DataSource dataSource;
    
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addUser(String name, int age) {
        String sql = String.format("INSERT INTO `user`(user_name, age) VALUES('%s', %d)", name, age);
        new JdbcTemplate(dataSource).update(sql);
        
        this.updateUserName(name + "_" + name);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserName(String name) {
        String sql = String.format("UPDATE `user` SET user_name = '%s'", name);
        new JdbcTemplate(dataSource).update(sql);
        
//        throw new RuntimeException("9965");
    }
}

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"
    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-4.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  

    <!-- 数据源 -->
    <bean id="mysql" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
        ...
    </bean>
    
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="mysql"/>
    </bean>
    
    <!-- 开启事务注解支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>  

    <context:component-scan base-package="cn.matt.transaction"/>  
</beans>  

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-context.xml")
public class BaseSpringTest {
    
}
public class TransactionConfigTest extends BaseSpringTest {
    
    @Autowired
    UserService userService;
    
    @Test
    @Transactional
    public void testTransactionConfig() {
        userService.addUser("jerry", 30);
    }
}

三、配置说明:

  • @RunWith:用于指定junit运行环境,spring提供SpringJUnit4ClassRunner作为Junit测试环境,方便使用spring的依赖注入
  • @ContextConfiguration:导入Spring配置文件
  • @Transactional:表示事务支持,指定方法执行完后自动回滚,可使用在类和方法上
    • 使用在方法上,表示该方法获得事务支持
    • 使用在类上,表示测试类的所有方法默认获得事务支持
  • @Rollback:事务回滚注解,默认为true,可省略,若需要提交事务,须设为false,如下:
public class TransactionConfigTest extends BaseSpringTest {
    @Autowired
    UserService userService;
    
    @Test
    @Transactional
    @Rollback(false)
    public void testTransactionConfig() {
        userService.addUser("jerry", 30);
    }
}

参考:

Spring整合Junit4进行单元测试

第十三章 测试 之 13.1 概述 13.2 单元测试 ——跟我学spring3

第十三章 测试 之 13.3 集成测试 ——跟我学spring3

猜你喜欢

转载自www.cnblogs.com/MattCheng/p/9037264.html