ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

项目中首次使用DBUnit拾遗

2021-04-26 07:01:43  阅读:221  来源: 互联网

标签:首次 DBUnit database 拾遗 import device org com class



文章目录


参考

正文

测试准备

测试数据准备

dev_equipment_info.xlsx - 测试数据

expected_dev_equipment_info.xlsx - 测试后数据

Excel数据准备注意点
  • 日期/时间格式 使用 'yyyy-mm-dd hh:mm:ss’格式
  • 每个sheet表的首行为数据表字段名称
  • sheet名称需要与表名完全相同,也可以使用database.tableName格式的sheet名
  • 一个excel数据表可以同时有多个数据表
  • Excel最后一行数据之后,不能只是清除内容,导入时无法识别

Maven 包导入

在pom.xml的dependencies中,增加

com.github.yangjianzhouspring-boot-unitils-starter1.3.0.RELEASE

Unitils额外配置(数据库连接)

application-ut.properties 存放于resources下,其内容可参考

database.driverClassName=com.mysql.jdbc.Driver
# 此数据库连接信息
database.url=jdbc:mysql://127.0.0.1/septictank_device
# 此数据库连接用户名
database.userName=root
# 此数据库连接用户密码
database.password=123456
# 此数据库连接的schema
database.schemaNames=septictank_device
# 此数据库数据库类型:oracle/mysql/postgres等
database.dialect=mysql
dataSource.beanName=druidDataSource

测试类配置 - BusDeviceTestApp

package com.citylink.device;

import com.unitils.boot.autoconfigure.ConfigurableApplicationContextAware;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.citylink.device"}, basePackageClasses =
        {ConfigurableApplicationContextAware.class, DataSourcePostProcessorExt.class})
public class BusDeviceTestApp {
    public static void main(String[] args) {
        SpringApplication.run(BusDeviceTestApp.class, args);
    }
}

主要实现以下功能:

  • basePackages = {“com.citylink.device”},扫描需要注入上下文的业务bean
  • ConfigurableApplicationContextAware.class,为测试提供上下文,以便@SpringBean,@SpringBeanByName,@SpringBeanByType等从上下文中注入bean
  • DataSourcePostProcessorExt.class,为替换系统中原有的dataSource bean的类,如测试时一直使用系统原数据库,很有可能是相应的dataSource未被正常替换
package com.citylink.device;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.unitils.database.UnitilsDataSourceFactoryBean;

import javax.sql.DataSource;

@Component
public class DataSourcePostProcessorExt implements BeanPostProcessor {

    public DataSourcePostProcessorExt() {
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof DataSource) {
            try {
                return (new UnitilsDataSourceFactoryBean()).getObject();
            } catch (Exception var4) {
                throw new RuntimeException("replace database throw exception ,can not continue to process", var4);
            }
        } else {
            return bean;
        }
    }
}

测试类 - DeviceLogServiceTest

package com.citylink.device;

import com.citylink.device.pojo.Device;
import com.citylink.device.service.IDeviceService;
import com.citylink.security.common.msg.ObjectRestResponse;
import com.unitils.boot.util.UnitilsBootBlockJUnit4Cla***unner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.unitils.dbunit.annotation.DataSet;
import org.unitils.dbunit.annotation.ExpectedDataSet;
import org.unitils.spring.annotation.SpringBeanByType;

import java.util.List;

@RunWith(UnitilsBootBlockJUnit4Cla***unner.class)
@SpringBootTest(classes = BusDeviceTestApp.class)
@DataSet("dev_equipment_info.xlsx")
public class DeviceLogServiceTest {
    @SpringBeanByType
    private IDeviceService deviceService;

    @Test
    @ExpectedDataSet("expected_dev_equipment_info.xlsx")
    public void testSelectAll() {
        ObjectRestResponse orr = deviceService.selectAll();
        Assert.assertTrue(orr.isRel());
        ListdeviceList = (List) orr.getData();
        System.out.println("deviceList.size()=" + deviceList.size());
    }
}

               

标签:首次,DBUnit,database,拾遗,import,device,org,com,class
来源: https://blog.51cto.com/u_15181029/2733642

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有